Multiline "when"
I just noticed this while coding along happily, and while it's not a big problem, it still kind of bugs me. Following code does not compile:
switch order
when "bread", "cake", "pizza", "water",
"rotten fish", "horn of a unicorn"
console.log "You receive a #{order}."
when "pistol", "machine gun",
"rocket launcher", "katana"
console.log "We don't sell any weapons."
I think it should be possible to break up when statements into multiple lines.
switch order
when "bread", "cake", "pizza", "water", \
"rotten fish", "horn of a unicorn"
console.log "You receive a #{order}."
when "pistol", "machine gun", \
"rocket launcher", "katana"
console.log "We don't sell any weapons."
It's not pretty, but it's something.
Looks like a bug to me. For now, you can use \ to continue the line.
$ coffee -bps
switch order
when "bread", "cake", "pizza", "water",\
"rotten fish", "horn of a unicorn"
console.log "You receive a #{order}."
when "pistol", "machine gun",\
"rocket launcher", "katana"
console.log "We don't sell any weapons."
switch (order) {
case "bread":
case "cake":
case "pizza":
case "water":
case "rotten fish":
case "horn of a unicorn":
console.log("You receive a " + order + ".");
break;
case "pistol":
case "machine gun":
case "rocket launcher":
case "katana":
console.log("We don't sell any weapons.");
}
edit: @erisdiscord beat me to it.
Use leading (instead of trailing) comma to suppress newline:
$ coffee -bsc
switch a
when b
, c
d
switch (a) {
case b:
case c:
d;
}
@satyr it's 2 years later but thank you for that!
is it possible to execute multiple statements on a 'when' match ?
@chinmay43 Yes!
switch 1
when 1
console.log 'statement 1'
console.log 'statement 2'
Seeing all the difficulties we’re having with leading periods for chaining, I’m not sure we want to be expanding the number of scenarios where we support multiline constructs. But if someone wants to submit a PR, it would be considered.