Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Jacob Bandes-Storch via swift-users
er, I guess it's probably plain ol' map, rather than flatMap. On Mon, Aug 1, 2016 at 8:28 PM, Jacob Bandes-Storch wrote: > You can think of this like flatMap: > > let count = s.flatMap { $0.characters.count } ?? 0// like > s?.characters.count ?? 0 > let count = s.flatMap { $0.charact

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Jacob Bandes-Storch via swift-users
You can think of this like flatMap: let count = s.flatMap { $0.characters.count } ?? 0// like s?.characters.count ?? 0 let count = s.flatMap { $0.characters }.flatMap { $0.count } ?? 0 // like (s?.characters)?.count ?? 0 Jacob On Mon, Aug 1, 2016 at 10:26 AM, Stephen Schaub via swi

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Ingo Maier via swift-users
Yes, parenthesized expressions are primary expressions, which in turn are all postfix expressions as specified by the first grammar rule for postfix expressions and also the note you mentioned. I do think that swiftc's behavior is the most intuitive. I just think the spec needs to be clarified in t

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Stephen Schaub via swift-users
Ingo, Thanks for pointing me to the specification. The example given in that section is helpful. 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 th

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Ingo Maier via swift-users
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 o

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Stephen Schaub via swift-users
Got it, I think! I didn't understand that the chaining of intermediate properties worked that way. Thanks very much for the explanation. Stephen On Mon, Aug 1, 2016 at 2:25 PM, Saagar Jha wrote: > When you write `(s?.characters).count`, the parentheses are evaluated > first; `(s?.characters)`

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Saagar Jha via swift-users
When you write `(s?.characters).count`, the parentheses are evaluated first; `(s?.characters)` gives an `String.CharacterView?`. Accessing the `String.CharacterView?`’s `count` property requires a `?`: `(s?.characters)?.count`. `s?.characters.count`, on the other hand, is applying chaining, whi

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Stephen Schaub via swift-users
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, Saa

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Saagar Jha via swift-users
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?.chara

Re: [swift-users] Optional chaining and String properties

2016-08-01 Thread Rimantas Liubertas via swift-users
> > 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? See print(s?.characters.count) — you get the optional des

[swift-users] Optional chaining and String properties

2016-08-01 Thread Stephen Schaub via swift-users
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: