Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a generic class with a constraint from a Protocol with a typealias in Swift?

I'm try to define a protocol P2 so that it returns a generic class with a constraint on another protocol P1, e.g:

protocol P1 {}

class C<T : P1> {}

public protocol P2 {
    typealias T
    class func c() -> C<T>
}

But this results in the following compiler error:

error: type 'T' does not conform to protocol 'P1'
    class func c() -> C<T>

There doesn't seem to be any combination that allows this, e.g. the next obvious syntax:

protocol P1 {}

class C<T : P1> {}

public protocol P2 {
    typealias T
    class func c() -> C<T : P1>
}

Errors with:

error: expected '>' to complete generic argument list
    class func c() -> C<T : P1>
                          ^
note: to match this opening '<'
    class func c() -> C<T : P1>

Is this possible to do in Swift?

like image 825
mythz Avatar asked Jan 26 '15 10:01

mythz


People also ask

How do I call a generic function in Swift?

Solution. A generic function that you might need to use explicit specialization is the one that infer its type from return type—the workaround for this by adding a parameter of type T as a way to inject type explicitly. In other words, we make it infer from method's parameters instead.

How do you declare a generic variable in Swift?

Generics allow you to declare a variable which, on execution, may be assigned to a set of types defined by us. In Swift, an array can hold data of any type. If we need an array of integers, strings, or floats, we can create one with the Swift standard library.

Can only be used as a generic constraint because it has self or associated type requirements?

Protocol 'SomeProtocol' can only be used as a generic constraint because it has Self or associated type requirements. Code that uses a protocol that relies on associated types pays the price. Such code must be written using generic types. Generic types are also placeholders.


1 Answers

I have never used constraints like that, but I think you can just define it in the typealias - I've tried it in a playground and compilation succeeds:

typealias T: P1
like image 135
Antonio Avatar answered Oct 20 '22 01:10

Antonio