[swift-evolution] Add unless/if single line syntax

Tommy van der Vorst tommy at pixelspark.nl
Mon Dec 14 06:31:40 CST 2015


Well that can be easily mitigated by adding an @warn_unused_result...

/T

> Op 14 dec. 2015, om 12:53 heeft Etan Kissling <kissling at oberon.ch <mailto:kissling at oberon.ch>> het volgende geschreven:
> 
> It's not the same, OP wants to return nil.
> 
> So you have to write 
> 
> return unless(username.text.characters.count > 0) {
>     return
> }
> 
> and if you miss the first return, you just introduced a bug.
> 
> 
> Etan
> 
>> On 12 Dec 2015, at 18:17, James Campbell via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> So 
>> 
>> guard username.text.characters.count > 0 else {
>>         return
>>     }
>> could be expressed as 
>> unless(username.text.characters.count > 0) {
>>         return
>>     }
>> I think the second is much easier to read :)
>> 
>> On Sat, Dec 12, 2015 at 4:28 PM, Al Skipp via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Top marks!
>> 
>> unless(condition) { print("condition was false") }
>> 
>> The feature implemented in the language. What’s not to like?
>> 
>>> On 12 Dec 2015, at 16:20, Tommy van der Vorst via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Oops, that should be just:
>>> 
>>> public func unless(expression: Bool, block: () -> ()) {
>>> 	if !expression {
>>> 		block()
>>> 	}
>>> }
>>> 
>>>> Op 12 dec. 2015, om 17:17 heeft Tommy van der Vorst via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> het volgende geschreven:
>>>> 
>>>> Or, use something like this:
>>>> 
>>>> public func unless(expression: @autoclosure () -> Bool, block: () -> ()) {
>>>> 	if !expression {
>>>> 		block()
>>>> 	}
>>>> }
>>>> 
>>>> unless(..expression..) { 
>>>> 	...code...
>>>> }
>>>> 
>>>> /T
>>>> 
>>>>> Op 12 dec. 2015, om 17:08 heeft Al Skipp via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> het volgende geschreven:
>>>>> 
>>>>> If at all possible I think it’s best to avoid adding keywords to the language. Instead of ‘unless', you could do: 
>>>>> 
>>>>> public func not(bool: Bool) -> Bool { return !bool }
>>>>> 
>>>>> if not(condition) { print("condition was false") }
>>>>> 
>>>>> Not terrible ¯\_(ツ)_/¯
>>>>> 
>>>>> 
>>>>>> On 12 Dec 2015, at 15:48, Matthew Johnson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>> 
>>>>>> I’m not sure about the single line aspect of this proposal, but I am a big proponent of “unless” and “until” as complements to “if” and “while”.  They greatly improve readability over "if !(expression)” and “while !(expression)” IMHO.
>>>>>> 
>>>>>>> On Dec 12, 2015, at 9:45 AM, Arthur Ariel Sabintsev via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>> 
>>>>>>> For your specific example, I'd 100% use the guard statement. It achieves the same result.
>>>>>>> 
>>>>>>> -1 from me.
>>>>>>> On Sat, Dec 12, 2015 at 9:43 AM J. Cheyo Jimenez via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>> -1 from me.  I think I would rather have an if-then expression that would handle both False and True cases. 
>>>>>>> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001944.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001944.html>
>>>>>>> 
>>>>>>> Also, in most cases I feel that guard would be a better choice. 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> On Sat, Dec 12, 2015 at 5:39 AM, James Campbell via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>> I propose adding the unless and if statements from ruby.
>>>>>>> 
>>>>>>> Return false if james = nil
>>>>>>> 
>>>>>>> This returns false if the variable James is nil
>>>>>>> . This line does it unless james is nil:
>>>>>>> 
>>>>>>> Return false unless james = nil
>>>>>>> 
>>>>>>> This has been really handy when working in ruby, reduces lines and pyramids of doom
>>>>>>> 
>>>>>>> Sent from my iPhone
>>>>>>> _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>>>> 
>>>>>>>  _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>>>>  _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>>> 
>>>>>>  _______________________________________________
>>>>>> swift-evolution mailing list
>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>>  _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> 
>> 
>> 
>> -- 
>>  Wizard
>> james at supmenow.com <mailto:james at supmenow.com>
>> +44 7523 279 698
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151214/21db4de4/attachment.html>


More information about the swift-evolution mailing list