> On 15 Aug 2016, at 08:02, Justin Jia via swift-evolution 
> <[email protected]> wrote:
> 
> Hi!
> 
> I don’t know if this has came up before. I tried to search though the mailing 
> list but didn’t find any related threads.
> 
> This is purely a syntactic thing (which I know it’s the lowest priority for 
> Swift 4), but I think it’s an important one.
> 
> Let’s say we have a struct with a function:
> 
> ```
> struct Foo {
>    func bar(x: Int)
> }
> ```
> 
> We can use optionals:
> 
> ```
> let foo: Foo? = nil
> let x = 1
> foo!.bar(x: x) // Able to compile, but will cause runtime error
> foo?.bar(x: x) // Able to compile, and won't cause runtime error
> ```
> 
> However:
> 
> ```
> let foo = Foo()
> let x: Int? = nil
> foo.bar(x: x!) // Able to compile, but will cause runtime error
> foo.bar(x: x?) // Won't compile
> ```
> 
> I propose that we should allow `foo.bar(x: x?)`, which should be equivalent 
> to:
> 
> ```
> if let x = x {
>  foo.bar(x: x)
> }
> ```
> 
> What do you think?

I like the intent behind this, but personally I think it's not clear enough. 
For me, putting the statement in a conditional as you've shown is the better 
solution, as it's a lot clearer exactly what's going on. Putting a question 
mark on a variable makes it look like something specific to that variable, 
rather than preventing the entire statement from executing.

There may be some alternatives though, for example, what about a shorthand for 
the conditional like so:

        if let x? { foo.bar(x: x) }
        if x? { foo.bar(x: x) } // even shorter?

But in general, I think it's best to be explicit about the entire statement 
being optional, which the conditional does but a postfix on a variable doesn't 
to the same degree.
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to