[swift-evolution] [Idea] Use optionals for non-optional parameters

Xiaodi Wu xiaodi.wu at gmail.com
Mon Aug 15 10:41:58 CDT 2016


What do you mean, limited to variables? What about a computed property? You
will have the same problem.

I'm not sure where you want to go with this, given that the core team has
considered the same idea in the past and found these issues to have no good
solution.

On Mon, Aug 15, 2016 at 04:56 Justin Jia <justin.jia.developer at gmail.com>
wrote:

> IMO I don't this bar should be evaluated unless we decide if let can
> accept non-optional values.
>
> Actually, what if we allow if let to accept non-optional values?
>
> I agree this is confusing at the beginning. But people who are not
> familiar with the detail design can avoid this situation easily. People who
> are familiar with the design can adopt it quickly. Sometimes, this is
> unavoidable.
>
> Btw, do you think this is still something nice to have if we limit this
> syntax to only variables?
>
> On Aug 15, 2016, at 4:59 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
> On Mon, Aug 15, 2016 at 3:55 AM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
>> On Mon, Aug 15, 2016 at 3:25 AM, Justin Jia via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>>
>>> On Aug 15, 2016, at 4:09 PM, Charlie Monroe <charlie at charliemonroe.net>
>>> wrote:
>>>
>>> The example above was to better demonstrate the problem with *when* to
>>> evaluate the latter argument. Why should both arguments be evaluated
>>> *before* the if statement? If both calls return Optionals,
>>>
>>> if let x = bar(42), y = baz(42) { ... }
>>>
>>> is how would I write it without the suggested syntax - baz(42) will
>>> *not* be evaluated if bar(42) returns nil. Which bears a question why would
>>>
>>> foo(bar(42)?, baz(42)?)
>>>
>>> evaluate both arguments even if the first one is nil, making it
>>> incosistent with the rest of the language?
>>>
>>>
>>> I see your point. I understand that maybe 1/2 of the people think we
>>> should evaluate both arguments and 1/2 of the people think we should only
>>> evaluate the first argument.
>>>
>>> I changed my idea a little bit. Now I think you are right. We should
>>> only evaluate the first argument in your example. It’s not only because of
>>> inconsistent, but also because the language should at least provide a way
>>> to “short-circuit” to rest of the arguments.
>>>
>>> If they want to opt-out this behavior, they can always write:
>>>
>>> ```
>>> let x = bar(42)
>>> let y = baz(42)
>>> foo(x?, y?)
>>> ```
>>>
>>
>> Well, that was just the easy part. Now, suppose bar is the function that
>> isn't optional.
>>
>> ```
>> foo(bar(42), baz(42)?)
>> ```
>>
>> Is bar evaluated if baz returns nil? If you want this syntax to be sugar
>> for if let, then the answer is yes.
>>
>
> s/yes/no/
>
>
>> If short-circuiting works left-to-right, then the answer is no.
>>
>
> s/no/yes/
>
> (See? Confusing.)
>
>
>> This is very confusing, and there is no good intuitive answer.
>>
>>
>>> _______________________________________________
>>> 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/20160815/bce3d432/attachment.html>


More information about the swift-evolution mailing list