<div dir="ltr">Ingo,<div><br></div><div>Thanks for pointing me to the specification. The example given in that section is helpful.</div><div><br></div><div>You make a good point about what constitutes "the outermost expression." Parenthesized expressions, according to the spec, are primary expressions, not postfix expressions, but the spec also states that "Syntactically, every primary expression is also a postfix expression." So, either I'm not understanding the spec correctly, or the compiler is handling this case differently for some reason.</div><div><br></div><div>Stephen</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 1, 2016 at 3:05 PM, Ingo Maier <span dir="ltr"><<a href="mailto:ingoem@gmail.com" target="_blank">ingoem@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If you are looking for the actual rules, they are defined in the<br>
language reference, in the section on optional chaining expressions<br>
[1]. It states that if you have an optional chaining expression of the<br>
form `expression?` then<br>
<br>
"If the value of the optional-chaining expression is nil, all of the<br>
other operations in the postfix expression are ignored and the entire<br>
postfix expression evaluates to nil. If the value of the<br>
optional-chaining expression is not nil, the value of the<br>
optional-chaining expression is unwrapped and used to evaluate the<br>
rest of the postfix expression. [...]. If a postfix expression that<br>
contains an optional-chaining expression is nested inside other<br>
postfix expressions, only the outermost expression returns an optional<br>
type."<br>
<br>
However, it seems to me that this is not quite a correct description<br>
of what swiftc does. Note the last sentence in particular. According<br>
to the grammar, given a postfix expression <e> and two identifiers x<br>
and y, the following are both postfix expressions:<br>
<br>
<e>?.x.y<br>
(<e>?.x).y<br>
<br>
Without further specification, I would consider "the outermost<br>
expression" to be `(<e>?.x).y` for the last case, when swiftc clearly<br>
stops at parentheses. The spec should distinguish between<br>
parenthesized-expressions and other postfix expressions or am I<br>
missing that part?<br>
<br>
[1] <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/optional-chaining-expression" rel="noreferrer" target="_blank">https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/optional-chaining-expression</a><br>
<br>
On Mon, Aug 1, 2016 at 8:17 PM, Stephen Schaub via swift-users<br>
<div class="HOEnZb"><div class="h5"><<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
> I understand that the String.characters property is not optional. However, I<br>
> am puzzled as to why<br>
><br>
> s?.characters.count<br>
><br>
> is legal, but<br>
><br>
> (s?.characters).count<br>
><br>
> is not. This seems counterintuitive. Can someone explain the logic or rules<br>
> being used here?<br>
><br>
> Stephen<br>
><br>
><br>
><br>
><br>
> On Mon, Aug 1, 2016 at 2:09 PM, Saagar Jha <<a href="mailto:saagar@saagarjha.com">saagar@saagarjha.com</a>> wrote:<br>
>><br>
>><br>
>> Saagar Jha<br>
>><br>
>> This isn’t quite how optional chaining in Swift works; see the Swift<br>
>> Programming Guide, specifically “Linking Multiple Levels of Chaining".<br>
>> Basically, `s?.characters.count` works because `s.characters` isn’t<br>
>> Optional. You only use ? on properties that are Optional.<br>
>><br>
>> On Aug 1, 2016, at 10:26, Stephen Schaub via swift-users<br>
>> <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
>><br>
>> With optional chaining, if I have a Swift variable<br>
>><br>
>> var s: String?<br>
>><br>
>> s might contain nil, or a String wrapped in an Optional. So, I tried this<br>
>> to get its length:<br>
>><br>
>> let count = s?.characters?.count ?? 0<br>
>><br>
>> However, the compiler wants this:<br>
>><br>
>> let count = s?.characters.count ?? 0<br>
>><br>
>> or this:<br>
>><br>
>> let count = (s?.characters)?.count ?? 0<br>
>><br>
>> My understanding of optional chaining is that, once you start using '?.'<br>
>> in a dotted expression, the rest of the properties evaluate as optional and<br>
>> are typically accessed by '?.', not '.'.<br>
>><br>
>> So, I dug a little further and tried this in the playground:<br>
>><br>
>> var s: String? = "Foo"<br>
>> print(s?.characters)<br>
>><br>
>> The result indicates that s?.characters is indeed an Optional instance,<br>
>> indicating that s?.characters.count should be illegal.<br>
>><br>
>> Why is s?.characters.count a legal expression?<br>
>><br>
>><br>
>> --<br>
>> Stephen Schaub<br>
>> _______________________________________________<br>
>> swift-users mailing list<br>
>> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
>><br>
>><br>
><br>
><br>
><br>
> --<br>
> Stephen Schaub<br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Stephen Schaub</div>
</div>