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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With