[swift-users] Optional chaining and String properties

Ingo Maier ingoem at gmail.com
Mon Aug 1 14:05:27 CDT 2016


If you are looking for the actual rules, they are defined in the
language reference, in the section on optional chaining expressions
[1]. It states that if you have an optional chaining expression of the
form `expression?` then

"If the value of the optional-chaining expression is nil, all of the
other operations in the postfix expression are ignored and the entire
postfix expression evaluates to nil. If the value of the
optional-chaining expression is not nil, the value of the
optional-chaining expression is unwrapped and used to evaluate the
rest of the postfix expression. [...]. If a postfix expression that
contains an optional-chaining expression is nested inside other
postfix expressions, only the outermost expression returns an optional
type."

However, it seems to me that this is not quite a correct description
of what swiftc does. Note the last sentence in particular. According
to the grammar, given a postfix expression <e> and two identifiers x
and y, the following are both postfix expressions:

<e>?.x.y
(<e>?.x).y

Without further specification, I would consider "the outermost
expression" to be `(<e>?.x).y` for the last case, when swiftc clearly
stops at parentheses. The spec should distinguish between
parenthesized-expressions and other postfix expressions or am I
missing that part?

[1] https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/optional-chaining-expression

On Mon, Aug 1, 2016 at 8:17 PM, Stephen Schaub via swift-users
<swift-users at swift.org> wrote:
> I understand that the String.characters property is not optional. However, I
> am puzzled as to why
>
> s?.characters.count
>
> is legal, but
>
> (s?.characters).count
>
> is not. This seems counterintuitive. Can someone explain the logic or rules
> being used here?
>
> Stephen
>
>
>
>
> On Mon, Aug 1, 2016 at 2:09 PM, Saagar Jha <saagar at saagarjha.com> wrote:
>>
>>
>> Saagar Jha
>>
>> This isn’t quite how optional chaining in Swift works; see the Swift
>> Programming Guide, specifically “Linking Multiple Levels of Chaining".
>> Basically, `s?.characters.count` works because `s.characters` isn’t
>> Optional. You only use ? on properties that are Optional.
>>
>> On Aug 1, 2016, at 10:26, Stephen Schaub via swift-users
>> <swift-users at swift.org> wrote:
>>
>> With optional chaining, if I have a Swift variable
>>
>>     var s: String?
>>
>> s might contain nil, or a String wrapped in an Optional. So, I tried this
>> to get its length:
>>
>>     let count = s?.characters?.count ?? 0
>>
>> However, the compiler wants this:
>>
>>     let count = s?.characters.count ?? 0
>>
>> or this:
>>
>>     let count = (s?.characters)?.count ?? 0
>>
>> My understanding of optional chaining is that, once you start using '?.'
>> in a dotted expression, the rest of the properties evaluate as optional and
>> are typically accessed by '?.', not '.'.
>>
>> So, I dug a little further and tried this in the playground:
>>
>> var s: String? = "Foo"
>> print(s?.characters)
>>
>> The result indicates that s?.characters is indeed an Optional instance,
>> indicating that s?.characters.count should be illegal.
>>
>> Why is s?.characters.count a legal expression?
>>
>>
>> --
>> Stephen Schaub
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>>
>
>
>
> --
> Stephen Schaub
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>


More information about the swift-users mailing list