Returns each type of generic class

It is possible to return any type of a generic class? Below is an example. Can I use the function "manager" to return any type of the generic class "Manager"? I know that the "some" keyword is not working at the inner of the "<>" statements. But I think you will know what I want to achieve with this.

// Base-Class
class Animal {
}

class Dog: Animal {
}

// Generic Class
class Manager<T: Animal> {
}

class DogManager: Manager<Dog> {
}

// Function which should return any generic class
func manager(for animal: Animal) -> Manager<some Animal> {
    return DogManager()
}
1 Like

If Manager either conforms to a protocol or has a concrete (non-generic) base class, you can use that as your return type. Otherwise, every version of Manager<something> is considered to be a separate type and there's no way to refer to them broadly.

1 Like

Okey, thanks.

Terms of Service

Privacy Policy

Cookie Policy