<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 9:30 AM, thorsten--- via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><font class=""><span class="" style="background-color: rgba(255, 255, 255, 0);">let ps = peeps.filter { $0.pet?.age.map { age in age &lt; 6 } ?? false }</span></font></div></div></div></div></div></blockquote></div></div></blockquote><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><br class=""></div></div></div></div></div></blockquote></div></div></div><div class=""><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">It’s curious that your example won’t compile (didn’t when I tried it in a Playground)</div></div></div></blockquote><div class=""><br class=""></div>You are right, the example does not compile (wrote it in the train far away from Xcode). It gives an error of „value of type ‚Int‘ has no member ‚map‘“.</div><div class=""><br class=""></div><div class="">Hmm, I consider that as a bug in the definition of optional chaining, as the documentation clearly states&nbsp;</div><div class="">"Optional chaining&nbsp;is a process for querying and calling properties, methods, and subscripts on an optional that might currently be&nbsp;nil. If the&nbsp;optional contains a value, the property, method, or subscript call succeeds; <b class="">if the optional is&nbsp;nil, the property, method, or subscript call returns&nbsp;nil.</b>“ (note the last sentence).</div><div class=""><br class=""></div><div class="">So if $0.pet?.age should return nil in case of $0.pet being nil, the type of $0.pet?.age has to be Int? and therefore understand map().<br class=""><br class=""><div style="margin: 0px; font-size: 13px; line-height: normal; color: rgb(211, 54, 130);" class=""><font face="Menlo" class="">peeps</font><span style="font-family: Menlo; color: rgb(131, 148, 150);" class="">[</span><span style="font-family: Menlo; color: rgb(41, 161, 152);" class="">0</span><span style="font-family: Menlo; color: rgb(131, 148, 150);" class="">].</span><font face="Menlo" class="">pet</font><span style="font-family: Menlo; color: rgb(131, 148, 150);" class="">?.</span><font face="Menlo" class="">age</font><span style="font-family: Menlo; color: rgb(131, 148, 150);" class="">.</span><span style="font-family: Menlo; color: rgb(133, 153, 1);" class="">dynamicType &nbsp; &nbsp; &nbsp;</span><span style="color: rgb(88, 110, 117);" class="">// =&gt; Int.Type this is wrong!! Should be Optional&lt;Int&gt;.Type</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; color: rgb(211, 54, 130);" class=""><div style="margin: 0px; line-height: normal; color: rgb(131, 148, 150);" class=""><span style="font-family: Menlo; color: rgb(133, 153, 1);" class="">let</span><font face="Menlo" class=""> y: </font><span style="font-family: Menlo; color: rgb(181, 137, 1);" class="">Int</span><font face="Menlo" class=""> = </font><span style="font-family: Menlo; color: rgb(211, 54, 130);" class="">peeps</span><font face="Menlo" class="">[</font><span style="font-family: Menlo; color: rgb(41, 161, 152);" class="">1</span><font face="Menlo" class="">].pet?.age</font><span style="font-family: Menlo; color: rgb(211, 54, 130);" class="">&nbsp; &nbsp; &nbsp;</span><span style="color: rgb(88, 110, 117);" class="">// This gives a type error which is right (but inconsistent with the dynamicType just claimed)</span></div></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="color: rgb(133, 153, 1); font-size: 13px; font-family: Menlo;" class="">let</span><font face="Menlo" style="color: rgb(131, 148, 150); font-size: 13px;" class=""> x = </font><span style="color: rgb(211, 54, 130); font-size: 13px; font-family: Menlo;" class="">peeps</span><font face="Menlo" style="color: rgb(131, 148, 150); font-size: 13px;" class="">[</font><span style="color: rgb(41, 161, 152); font-size: 13px; font-family: Menlo;" class="">1</span><font face="Menlo" style="color: rgb(131, 148, 150); font-size: 13px;" class="">].</font><span style="color: rgb(211, 54, 130); font-size: 13px; font-family: Menlo;" class="">pet</span><font face="Menlo" style="color: rgb(131, 148, 150); font-size: 13px;" class="">?.</font><span style="color: rgb(211, 54, 130); font-size: 13px; font-family: Menlo;" class="">age &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><font color="#586e75" size="2" class="">// Now let’s see what the type is after assigning the expression</font></div><div style="margin: 0px; font-size: 13px; line-height: normal; color: rgb(133, 153, 1);" class=""><span style="font-family: Menlo; color: rgb(211, 54, 130);" class="">x</span><span style="font-family: Menlo; color: rgb(131, 148, 150);" class="">.</span><font face="Menlo" class="">dynamicType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</font><span style="color: rgb(88, 110, 117);" class="">// =&gt;</span><span style="color: rgb(88, 110, 117);" class="">&nbsp;Optional&lt;Int&gt;.Type as expected</span></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; color: rgb(131, 148, 150);" class=""><span style="font-family: Menlo; color: rgb(211, 54, 130);" class="">x</span><font face="Menlo" class="">.</font><span style="font-family: Menlo; color: rgb(108, 113, 196);" class="">map</span><font face="Menlo" class=""> { age </font><span style="font-family: Menlo; color: rgb(133, 153, 1);" class="">in</span><font face="Menlo" class=""> age &lt; </font><span style="font-family: Menlo; color: rgb(41, 161, 152);" class="">6</span><font face="Menlo" class=""> } ?? </font><span style="font-family: Menlo; color: rgb(133, 153, 1);" class="">false &nbsp;</span><span style="color: rgb(88, 110, 117);" class="">// =&gt;</span><span style="color: rgb(88, 110, 117);" class="">&nbsp;false as expected</span></div></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="color: rgb(88, 110, 117);" class=""><br class=""></span></div><div class="">So, obviously a bug in the compiler.</div><div class=""><br class=""></div></div></div></div></blockquote><br class="">This works:&nbsp;<br class=""><br class="">let ps = peeps.filter { ($0.pet?.age).map { age in age &lt; 6 } ?? false }<br class=""><br class="">Essentially the “?.” operator unwraps the optional, and any further “.” in the chain get applied with the non-optional types as long as the results are non-nil. In order to get an optional result again, you need to break the “.” chain, as with the parens in this case.<br class=""><br class="">I’m not sure: is this actually a compiler bug, and the type constraint system ought to consider binding to members of “T?" as well as “T" at each further “.”, or is this an expected (but obscure) part of the language, in which case the bug is maybe just that if no members match for “T" the error could suggest this paren fix if there is a member match for “T?”?<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>- Greg</div><br class=""></body></html>