Poll for a Crystal keyword I never use it

  • Do you know there is a then keyword in Crystal?
  • Yes
  • No
0 voters

  • Have you ever used it?
  • Yes
  • No
0 voters

  • Do you often use it?
  • Yes
  • No
0 voters

Let’s see if this is a failed design?

I use “then.”
I like that “when” is only one line.

case c
when 65u8, 97u8  then 0u8 # A
when 67u8, 99u8  then 1u8 # C
when 71u8, 103u8 then 2u8 # G
when 84u8, 116u8 then 3u8 # T
when 78u8, 110u8 then 4u8 # N
else
  STDERR.puts "[sdust] '#{c.chr}' is replaced with 'N'"
  4u8
end
4 Likes

Yes, the use case for then is one-liner when branches.

Of course, a statement separator works as well, so you could replace then with ; for even more concise syntax.
So it’s not strictly necessary, but still reads a bit nicer IMO.

case c
when 65u8, 97u8  ; 0u8 # A
when 67u8, 99u8  ; 1u8 # C
when 71u8, 103u8 ; 2u8 # G
when 84u8, 116u8 ; 3u8 # T
when 78u8, 110u8 ; 4u8 # N
else
  STDERR.puts "[sdust] '#{c.chr}' is replaced with 'N'"
  4u8
end
2 Likes

I use it all the time to make more concise-looking case statements. I drop it when I need to use multiple lines in any of my cases, or if the lines get too unreadable.

1 Like

Not only I use it, I wish it was available for if as well. I’d rather use then instead of a semicolon in one-line if statements or expressions.

5 Likes