[swift-evolution] [Idea] Use optionals for non-optional parameters
Charlie Monroe
charlie at charliemonroe.net
Mon Aug 15 02:17:49 CDT 2016
Hi Justin,
IIRC, there were several discussions about this, all of them ending up at a dead point. The question was, what would be the behavior of
foo(argA: methodA(), argB: methodB())
where methodA returns an optional. If that evaluates to nil, would methodB get evaluated as well? Or should it shortcircuit?
> On Aug 15, 2016, at 9:02 AM, Justin Jia via swift-evolution <swift-evolution at swift.org> 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?
>
> Thanks,
> Justin
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list