Alternative For Nested If Repetiion


(Ted van Gaalen) #1

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


(Kurt Werle) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/


(Kenny Leung) #3

Very nice!

-Kenny

···

On Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution <swift-evolution@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #4

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 Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution <swift-evolution@swift.org> wrote:

On Sat, Mar 12, 2016 at 1:17 PM, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Kurt Werle) #5

Oooh - didn't know about that - very nice.

Thanks,
Kurt

···

On Mon, Mar 14, 2016 at 10:31 AM, Joe Groff <jgroff@apple.com> wrote:

On Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution < > swift-evolution@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org <kurt@circlew.org>
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/


(Ted van Gaalen) #6

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@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@apple.com <mailto:jgroff@apple.com>> wrote:

On Mar 12, 2016, at 3:20 PM, Kurt Werle via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org <mailto:kurt@circlew.org>
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>


(Joe Groff) #7

If the "true" bothers you, you can also switch over "nothing" and use where clauses:

switch () {
case () where cond1: ...
case () where cond2: ...
}

It might be interesting to consider a shorthand:

switch {
where cond1: ...
where cond2: ...
}

-Joe

···

On Mar 14, 2016, at 11:25 AM, Ted F.A. van Gaalen <tedvgiosdev@gmail.com> wrote:

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


(Ted van Gaalen) #8

.. or maybe just leave out the switch operand like so:

     switch
    {
          case if a > b:
                   c = 1234

          case c == d:
                  a = b

          default:
                 doSomething()
    }

?

TedvG

···

On 14.03.2016, at 19:27, Joe Groff <jgroff@apple.com> wrote:

On Mar 14, 2016, at 11:25 AM, Ted F.A. van Gaalen <tedvgiosdev@gmail.com> wrote:

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

If the "true" bothers you, you can also switch over "nothing" and use where clauses:

switch () {
case () where cond1: ...
case () where cond2: ...
}

It might be interesting to consider a shorthand:

switch {
where cond1: ...
where cond2: ...
}

-Joe


(Kenny Leung) #9

I don’t see how this is significantly different from

if a > b {
    c = 1234
} else if c == d {
    a = b
} else {
    doSomething()
}

If you put them side by side, it’s pretty much equivalent in size and shape, except for a few braces.

-Kenny

···

On Mar 14, 2016, at 11:41 AM, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org> wrote:

.. or maybe just leave out the switch operand like so:

    switch
   {
         case if a > b:
                  c = 1234

         case c == d:
                 a = b

         default:
                doSomething()
   }

?

TedvG

On 14.03.2016, at 19:27, Joe Groff <jgroff@apple.com> wrote:

On Mar 14, 2016, at 11:25 AM, Ted F.A. van Gaalen <tedvgiosdev@gmail.com> wrote:

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

If the "true" bothers you, you can also switch over "nothing" and use where clauses:

switch () {
case () where cond1: ...
case () where cond2: ...
}

It might be interesting to consider a shorthand:

switch {
where cond1: ...
where cond2: ...
}

-Joe

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution