coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Multiline "when"

Open copygirl opened this issue 14 years ago • 7 comments

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.

copygirl avatar Jan 08 '12 01:01 copygirl

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.

erisdev avatar Jan 08 '12 01:01 erisdev

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.

michaelficarra avatar Jan 08 '12 01:01 michaelficarra

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 avatar Jan 08 '12 14:01 satyr

@satyr it's 2 years later but thank you for that!

omnikron avatar Feb 12 '14 16:02 omnikron

is it possible to execute multiple statements on a 'when' match ?

chinmay43 avatar Nov 05 '15 12:11 chinmay43

@chinmay43 Yes!

switch 1
  when 1
      console.log 'statement 1'
      console.log 'statement 2'

lydell avatar Nov 05 '15 13:11 lydell

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.

GeoffreyBooth avatar May 05 '17 07:05 GeoffreyBooth