<div dir="ltr">If not using '?' at arbitrary positions, but instead applying '?' to the function call, it looks like it's possible to enable OK syntax by defining a custom operator (at least for unnamed args):<div><span style="color:rgb(150,152,150);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">let result = fn<-?(a, b)</span></div><div>Here:</div><div> - fn is a function that has two non-optional parameters</div><div> - a and b are optionals of the same type as the parameters</div><div> - fn is called if both a and b are non-nil</div><div> - result is nil if either a or b are nil, otherwise it's the value returned by calling fn</div><div><br></div><div>Noodles at:</div><div><a href="https://gist.github.com/callionica/11cb880c1752b1098b012d67d693d9cc">https://gist.github.com/callionica/11cb880c1752b1098b012d67d693d9cc</a><br></div><div><br></div><div>Kind of feels like one of the functional operator libraries should already have operators that can optionalize non-optional functions, so I'd look there first.</div><div><br></div><div>-- Callionica</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 13, 2016 at 10:37 AM, Joe Groff via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Dec 12, 2016, at 4:15 PM, John Holdsworth via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> As the festive season approaches I thought I’d write a brain dump<br>
> of a few things I'd hope Santa could bring in his bag for Swift 3.1.<br>
> No big ideas, just things that bug me day to day and would be<br>
> backward source compatible.<br>
><br>
> I’m sure it’s been discussed before but I wish static and class vars and<br>
> functions were in scope for instance methods without having to prefix them<br>
> as they are in Java. I’d go further and say they should be available when<br>
> referring to an object outside the class as if they were instance methods<br>
> i.e. object.classMethod().<br>
><br>
> Occasionally I find myself wishing I could give a static variable function<br>
> or method scope i.e. declare it inside the function or method if there are<br>
> no other references to localise access.<br>
><br>
> I’d like to raise again the idea of optionality when referencing a key or<br>
> calling a function could be possible using a ? i.e instead of<br>
><br>
> let a = key != nil ? dict[key] : nil<br>
><br>
> you could just write:<br>
><br>
> let a = dict[key?]<br>
><br>
> or even<br>
><br>
> let a = func( arg: argumentThatMayBeNull? ) // not called if argument is nil<br>
><br>
> As subscripts are functions these are probably the same thing.<br>
<br>
</span>Allowing `?` at an arbitrary position like this has ambiguity problems, since it isn't clear how much of the enclosing expression needs to be conditionalized on the unwrapped optional. This would be hard for humans to read, and it'd be an exponential search for the compiler to find a solution that works. You can use Optional's `map` and `flatMap` to chain an arbitrary closure, though. `dict[key?]` would be `key.flatMap { dict[$0] }`, and `func(arg: optional?)` would be `optional.flatMap { func(arg: $0) }`.<br>
<br>
-Joe<br>
<div class="HOEnZb"><div class="h5"><br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</div></div></blockquote></div><br></div>