[swift-evolution] It's the little things..

Joe Groff jgroff at apple.com
Tue Dec 13 12:37:56 CST 2016


> 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



More information about the swift-evolution mailing list