[swift-evolution] [Pitch] Make `return` optional in computed properties for a single case

Vladimir.S svabox at gmail.com
Tue May 31 15:14:39 CDT 2016


On 31.05.2016 22:42, Leonardo Pessoa via swift-evolution wrote:
> I'd actually like to see a change in guard so that I don't need those
> braces. I'd like something more readable like
>
> |   guard cond1 or return nil
> |   guard cond2 or throw MyError.IllegalValue
> |   guard cond3 or do { ... }

I partially support this, but don't like this 'or'..
For me 'otherwise' or 'else' is better, but as soon as braces after `else` 
is required in `if` statement, for `guard` we need another word to be able 
to omit braces...

guard cond otherwise return
guard cond otherwise return nil
guard cond otherwise throw MyError.IllegalValue
guard cond otherwise do { ... }

But I feel like this will be inconsistent with other parts of language, 
like `if` (don't you want to be able to do `if cond return nil`?), so I 
don't think it's worth to discuss this.

Hmm.. just as thought experiment :

if cond return nil else do {...}

if cond do {...} else return nil

if cond throw MyError.IllegalValue else return nil

if cond do {...} else do {...}

>
> It may add more cases for the compiler to handle but in all cases I
> used guard so far the block was never really needed. But I think this
> is out of the scope of this thread.
>
> L
>
> On 31 May 2016 at 15:59, Adrian Zubarev via swift-evolution
> <swift-evolution at swift.org> wrote:
>> +1. This is example *is not* a single expression code block. There are 3
>> expressions (the condition, the return value in the else block, and the
>> primary return value).
>>
>> The `else` block is a returning single expression block. I can’t show the
>> `guard` example without any returning scope.
>>
>> You said it yourself "everywhere in the language“. It’s not “everywhere“ if
>> we would left out `guards` else-returning block.
>>
>> If we’d allow this we could also write:
>>
>> func test(boolean: Bool) {
>>     guard boolean else {}
>>     print("true")
>> }
>>
>> This is useless and less readable.
>>
>> But we already can do this with closures:
>>
>> let nop = {} // useless
>>
>> switch value {
>>    ...
>>    default: {}() // do nothing
>> }
>>
>> --
>> Adrian Zubarev
>> Sent with Airmail
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
> _______________________________________________
> 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