[swift-evolution] It's the little things..
Callionica (Swift)
swift-callionica at callionica.com
Tue Dec 13 13:43:51 CST 2016
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):
let result = fn<-?(a, b)
Here:
- fn is a function that has two non-optional parameters
- a and b are optionals of the same type as the parameters
- fn is called if both a and b are non-nil
- result is nil if either a or b are nil, otherwise it's the value
returned by calling fn
Noodles at:
https://gist.github.com/callionica/11cb880c1752b1098b012d67d693d9cc
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.
-- Callionica
On Tue, Dec 13, 2016 at 10:37 AM, Joe Groff via swift-evolution <
swift-evolution at swift.org> wrote:
>
> > On Dec 12, 2016, at 4:15 PM, John Holdsworth via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > As the festive season approaches I thought I’d write a brain dump
> > of a few things I'd hope Santa could bring in his bag for Swift 3.1.
> > No big ideas, just things that bug me day to day and would be
> > backward source compatible.
> >
> > I’m sure it’s been discussed before but I wish static and class vars and
> > functions were in scope for instance methods without having to prefix
> them
> > as they are in Java. I’d go further and say they should be available when
> > referring to an object outside the class as if they were instance methods
> > i.e. object.classMethod().
> >
> > Occasionally I find myself wishing I could give a static variable
> function
> > or method scope i.e. declare it inside the function or method if there
> are
> > no other references to localise access.
> >
> > I’d like to raise again the idea of optionality when referencing a key or
> > calling a function could be possible using a ? i.e instead of
> >
> > let a = key != nil ? dict[key] : nil
> >
> > you could just write:
> >
> > let a = dict[key?]
> >
> > or even
> >
> > let a = func( arg: argumentThatMayBeNull? ) // not called if
> argument is nil
> >
> > As subscripts are functions these are probably the same thing.
>
> 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)
> }`.
>
> -Joe
>
> _______________________________________________
> 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/20161213/d196253f/attachment.html>
More information about the swift-evolution
mailing list