How to return generic class with opaque type

Den Andreychuk

How can I return opaque type for generic class?

func makeFAQController() -> UIHostingController<some View> {
    let viewModel = FAQViewModel()
    let view = FAQView().environmentObject(viewModel)
    let controller = UIHostingController(rootView: view)
    return controller
}

Error: Cannot convert value of type 'UIHostingController<some View>' to type 'UIHostingController<FAQView>' in coercion

func makeFAQController() -> UIHostingController<FAQView> {
    let viewModel = FAQViewModel()
    let view = FAQView().environmentObject(viewModel)
    let controller = UIHostingController(rootView: view)
    return controller
}

Cannot convert value of type 'UIHostingController<some View>' to type 'UIHostingController<FAQView>' in coercion

I want to avoid type erasure if possible.

Asperi

You should return base class (there is nothing more generic then base class, probably only meta NSObject), so

func makeFAQController() -> UIViewController {    // << here !!
    let viewModel = FAQViewModel()
    let view = FAQView().environmentObject(viewModel)
    let controller = UIHostingController(rootView: view)
    return controller
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to return class type of a generic class with generic?

How to return the Class of a generic type

Generic type parameters C# - How to generic class return type

How to force type on return value in a generic class?

How to force type on return value in a generic class?

How to return instance of concrete class from method with generic return type

Return an opaque type of array

Stream return class type generic

Opaque Type with Class

Delegate for generic class and method with generic return type

How does getClass().getName() return class name of the generic type?

How to explicitly define the return type of a generic returned class in TypeScript?

mypy: How to declare the return type of a method returning self in a generic class?

How can I return a class instance with a generic type as an interface with a generic type?

Using type variable as a return type in a generic class

How to set a generic return type

How to return a generic type in TypeScript

How to create generic method to return generic type

Java generic function: how to return Generic type

Return class of generic type in default interface method

Return a Class instance with its generic type

Getting class of return generic type possible?

Method return type using enclosed Generic class?

Typescript: Wrap function return type of generic class

How to get Class for generic type?

How to determine the class of a generic type?

How to define a generic type that is not a class?

How to explain the type of return value and parameter definition at sub class in a generic class hierarchy?

How to return a generic type from a nested class using a method in the main class