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

Xiaodi Wu xiaodi.wu at gmail.com
Mon Aug 15 17:40:00 CDT 2016


To what benefit?


On Mon, Aug 15, 2016 at 16:23 Charles Srstka <cocoadev at charlessoft.com>
wrote:

> On Aug 15, 2016, at 3:08 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
>
> On Mon, Aug 15, 2016 at 3:05 PM, Charles Srstka <cocoadev at charlessoft.com>
>  wrote:
>
>> On Aug 15, 2016, at 2:27 PM, Xiaodi Wu via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>
>> `let value = (x == nil) ? nil : foo.bar(x: x)` isn't so bad, is it? You
>> could even write a custom operator to sugar it.
>>
>>
>> It’s distasteful, due to the need to use the force-unwrap operator. In
>> cases like this, I usually end up writing:
>>
>> let value: Foo? = nil
>>
>> if let x = x {
>> value = foo.bar(x: x)
>> } else {
>> value = nil
>> }
>>
>> or:
>>
>> let value: Foo? = {
>> if let x = x {
>> return foo.bar(x: x)
>> } else {
>> return nil
>> }
>> }()
>>
>> Both of which are unwieldy, but necessary to avoid the use of !.
>>
>
> You are arguing that the force unwrap operator ! is, per se, distasteful?
>
>
> If there are two ways to do something, and one uses the force-unwrap
> operator while the other does not, I’ll usually opt for the latter.
>
> Charles
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160815/8c84fe1e/attachment.html>


More information about the swift-evolution mailing list