[swift-evolution] [Pitch] Parse expressions after 'as' and 'is' instead of just types
Matthew Johnson
matthew at anandabits.com
Wed May 18 15:01:20 CDT 2016
> On May 18, 2016, at 2:55 PM, Joe Groff <jgroff at apple.com> wrote:
>
>>
>> On May 18, 2016, at 12:54 PM, Matthew Johnson <matthew at anandabits.com> wrote:
>>
>>
>>> On May 16, 2016, at 4:06 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> Currently, we parse a type after 'as[?!]' and 'is'. This is mostly what you'd expect, but does lead to problems when an 'as' expression appears as part of a comparison:
>>>
>>> 20 as Int64 < y as Int64 // error, '>' expected to close generic parameter list Int64<y>
>>>
>>> Looking to the future, many people have also expressed interest in the ability to do dynamic type checks against metatype values, not only static types, as in:
>>>
>>> class Base {}
>>> class DerivedA {}
>>> class DerivedB {}
>>>
>>> var x: Base.Type = DerivedA
>>>
>>> DerivedA() as? x // succeeds
>>> DerivedB() as? x // fails
>>
>> The dynamic check itself makes sense but it isn’t clear what type these expressions should return? It can’t be `DerivedA?` like it would in the equivalent static expression as the type is not known statically:
>>
>> DerivedA() as? DerivedA // succeeds with value Optional<DerivedA>.Some
>> DerivedB() as? DerivedA // fails with value Optional<DerivedA>.None
>
> Right, the static type of a cast like this would still have to be the upper bound type of the metatype value. (Unless we introduce some sort of path-dependent types to allow `x.Self` to be a type.)
So `Base?` in this case? That makes sense. Thanks for clarifying.
>
> -Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160518/ba26c5f5/attachment.html>
More information about the swift-evolution
mailing list