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
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
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
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
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
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
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
.. 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: ...
}
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: ...
}