[swift-evolution] Alternative For Nested If Repetiion
Ted F.A. van Gaalen
tedvgiosdev at gmail.com
Mon Mar 14 13:25:50 CDT 2016
Unexpected ways :o)
All very nice and well but readable?
I was looking a new language element
that would be similar to
Switch true ….
but then limited for cases
which are logical expressions only.
Ok, Perhaps it is not that important.
I will forget it and will continue
to use “switch true” case <logical expression>: etc.
sorry I brought it forward.
TedvG
On 14.03.2016, at 18:52, Kurt Werle <kurt at circlew.org> wrote:
>
> Oooh - didn't know about that - very nice.
>
> Thanks,
> Kurt
>
> On Mon, Mar 14, 2016 at 10:31 AM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>
>> On Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> let x = 1
>> let y = 2
>> let z = 3
>>
>> let condition1 = x > y
>> let condition2 = z > x
>>
>> switch (condition1, condition2) { // Treat your conditions as peers
>> case (true, _): // true & whatever
>> break
>> case (false, true): // false, true
>> break
>> case (false, false): // false, false
>> break
>> // no default needed - we covered all the cases
>> }
>>
>
> You can also use tuple labels here to aid readability:
>
> switch (x_y: x > y, z_x: z > x) {
> case (x_y: true, z_x: _):
> ...
> }
>
> -Joe
>
>>
>> On Sat, Mar 12, 2016 at 1:17 PM, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> a block of nested 'if else’ is not really nice, i think:
>>
>> • if condition 1 {
>> • statements to execute if condition 1 is true
>> • } else if condition 2 {
>> • statements to execute if condition 2 is true
>> • } else {
>> • statements to execute if both conditions are false
>> • }
>>
>> what I currently do to prevent this:
>>
>> switch true
>> {
>> case film == 2010:
>> itsFullOfStars(monolith)
>>
>> case terriblyWrongPresident():
>> initiateNewElections()
>>
>> case b > c:
>> c = b
>>
>> case d > c:
>> c = d
>>
>> default:
>> break // or something else to do, if all other conditions are fase
>> }
>>
>>
>> This works perfectly well, looks good too, I think, but is it right thing to do it this way,
>> as in principle 'switch’ is perhaps more intended to evaluate variables?
>>
>> Python has its “elif” keyword…
>>
>> So what about, an ‘ifs’ statement group in Swift:
>>
>>
>> ifcases // or another keyword, perhaps
>> {
>> case a > b:
>> b = a
>> case c < d:
>> b = c
>> }
>> without fall-through option.
>> perhaps also without default
>>
>>
>> What do you think?
>>
>> TedvG
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> 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>
>>
>>
>>
>> --
>> kurt at CircleW.org <mailto:kurt at circlew.org>
>> http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
>> _______________________________________________
>> 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>
>
>
>
>
> --
> kurt at CircleW.org
> http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160314/ee44b4a6/attachment.html>
More information about the swift-evolution
mailing list