-
Notifications
You must be signed in to change notification settings - Fork 1
Switch
justinmann edited this page Dec 25, 2017
·
1 revision
The switch operator is simply a cleaner looking version of and if/else block. The first condition that matches win and all following condition checks are not executed. If you want to return a value from your switch block you will need a "default" clause.
x : 0
switch x {
0 { /* x == 0 */ }
1 { /* x == 1 */ }
}
// Or you can be explicit about the comparison
// using the _ to represent the value x
switch x {
_ == 0 { /* x == 0 */ }
_ > 0 { /* x > 0 */ }
}
// Or skip the switch variable
switch {
x == 0 { /* x == 0 */ }
y == 1 { /* y == 1 */ }
}
// You can use a switch block to return a value
canYouReadThis : switch {
"yes" != "no" { "of course" }
default { "how did that happen?" }
}