[swift-evolution] PITCH: New :== operator for generic constraints

Brent Royal-Gordon brent at architechies.com
Fri Aug 19 05:30:11 CDT 2016


> On Aug 16, 2016, at 6:51 PM, Slava Pestov via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> I am a little concerned about the second requirement. Protocols that include static methods and initializers work perfectly well inside arrays, and restricting them from generic collections will further discourage use of the latter in favor of the former.
> 
> Here is why we must have that requirement. Consider the following code:
> 
> protocol P {
>   init()
> }
> 
> struct A : P {
>   init() {}
> }
> 
> struct B : P {
>   init() {}
> }
> 
> func makeIt<T : P>() -> T {
>   return T()
> }
> 
> I can use this function as follows:
> 
> let a: A = makeIt() // Creates a new ‘A'
> let a: B = makeIt() // Creates a new ‘B’
> 
> Now suppose we allow P to self-conform. Then the following becomes valid:
> 
> let p: P = makeIt()
> 
> What exactly would makeIt() do in this case? There’s no concrete type passed in, or any way of getting one, so there’s nothing to construct. The same issue would come up with static methods here.

Could we mark the generic parameter with an attribute which basically means "I will not call static members of this type except through a `type(of:)` call"?

For that matter, should we just do that by default? It seems like in most cases where we don't somehow pass an instance of the type, we end up passing an instance of its metatype anyway. See, for instance, `unsafeBitCast(_:to:)`, which takes an instance of the metatype which is technically redundant, but helps pin down the return type. Anything you might need to do with `U.self` could instead be done with `to`.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list