[swift-users] Weird protocol behaviour.
Kevin Nattinger
swift at nattinger.net
Thu Dec 22 14:29:47 CST 2016
I recall seeing a request on the -evolution list for something like `T := X` to indicate it could be X itself or anything inheriting / implementing it, so it’s certainly known behavior, if not desired. IMO it’s a bug and `:` should be fixed to include the root type, whether or not that requires a discussion on -evolution.
> On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users <swift-users at swift.org> wrote:
>
> I suspect a compiler bug since A is a P. The equivalent in Java works:
>
> interface P {}
> class X implements P {}
>
> <A extends P> void foo(A x) {}
>
> void bar() {
> final P x = new X();
> foo(x);
> }
>
> -- Howard.
>
> On 23 Dec 2016, at 3:19 am, Rien via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>
>> IMO the error message says it all:
>>
>> Playground execution failed: error: MyPlayground8.playground:9:5: error: cannot invoke 'foo' with an argument list of type '(P)'
>> foo(x)
>> ^
>>
>> MyPlayground8.playground:9:5: note: expected an argument list of type '(A)'
>> foo(x)
>> ^
>>
>> I.e. you are passing in a protocol while the function is specified for a type.
>> Said other way: On which data do you expect the protocol to operate?
>>
>> Regards,
>> Rien
>>
>> Site: http://balancingrock.nl <http://balancingrock.nl/>
>> Blog: http://swiftrien.blogspot.com <http://swiftrien.blogspot.com/>
>> Github: http://github.com/Swiftrien <http://github.com/Swiftrien>
>> Project: http://swiftfire.nl <http://swiftfire.nl/>
>>
>>
>>
>>
>>> On 22 Dec 2016, at 17:05, Mikhail Seriukov via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>
>>> Hello community! I' wondering if somebody can explain this to me.
>>> Please take look at the snippet.
>>>
>>> protocol P {}
>>> struct X:P {}
>>>
>>> func foo<A:P>(_ x:A) {}
>>>
>>> func bar() {
>>> //let x = X() // this compiles
>>> let x = X() as P // this does not compile. Why?
>>> foo(x)
>>> }
>>>
>>> I expect the both cases to work though. But only first works? And I do not understand why.
>>> My coworkers said that it is a compiler bug, but I'm not shure it is.
>>> Thanks for the help.
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161222/71c5222c/attachment.html>
More information about the swift-users
mailing list