[swift-evolution] Alternative For Nested If Repetiion

Kenny Leung kenny_leung at pobox.com
Sat Mar 12 17:24:58 CST 2016


Very nice!

-Kenny


> On Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution <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
> }
> 
> 
> 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/
> _______________________________________________
> 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