[swift-evolution] Optional Argument Chaining

C. Keith Ray keithray at mac.com
Mon Dec 11 11:37:11 CST 2017

You can create a binary operator that tests the left-hand operand for nil, and passes the unwrapped value to the right-hand operand (a function taking one value), this operator can be made left-associative to allow chaining.

let m = String(contentsOfFile: "README.md") ??? Markdown 
where ??? is the operator described above.

C. Keith Ray

* https://leanpub.com/wepntk <- buy my book?
* http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf
* http://agilesolutionspace.blogspot.com/

> On Dec 11, 2017, at 9:07 AM, Magnus Ahltorp via swift-evolution <swift-evolution at swift.org> wrote:
>> 12 Dec. 2017 01:30 Jared Khan via swift-evolution <swift-evolution at swift.org> wrote:
>> I'd like to propose a syntax addition that acts to ease some things that I believe should fall under the umbrella of 'optional chaining'. Optional chaining allows us to access the properties of an optional value and return nil if any link in that chain breaks. I propose we introduce syntax to allow similar chaining when passing optional valued parameters to functions that expect that parameter to be non-optional.
> 1. Am I right in assuming that you propose that the suffix operator "?" would make the result of the surrounding method/function call optional, so that a(b(c?)) would make the result of the "b" function call optional, but not the "a" function call, and that it would be a(b(c?)?) if we would like to propagate this two levels?
> 2. If that is the case, is that understandable/neat enough? How common would you expect this to be?
> 3. For some reason, (in current Swift) the suffix operator "?" seems to be allowed in intra-expression syntax, and only fails when the inter-expression syntax is checked for optionality congruence. Is there a reason for this? I would have expected that the congruence error "cannot use optional chaining on non-optional value of type" would never be seen for a lone "?", since the error message "'?' must be followed by a call, member lookup, or subscript" would always be displayed first if it was checked first. The "." operator checks intra-expression syntax first, before checking congruence. Is this a sign that "?" as a suffix operator is already somewhat operational as an operator for optional types? I have a faint recollection that it was doing something in earlier versions of Swift.
> /Magnus
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171211/37e996ed/attachment.html>

More information about the swift-evolution mailing list