[swift-evolution] Alternative For Nested If Repetiion
Kurt Werle
kurt at circlew.org
Sat Mar 12 17:20:47 CST 2016
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
}
On Sat, Mar 12, 2016 at 1:17 PM, Ted F.A. van Gaalen via swift-evolution <
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
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
--
kurt at CircleW.org
http://www.CircleW.org/kurt/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160312/ca1492be/attachment.html>
More information about the swift-evolution
mailing list