I'm writing an (interpolated) string which has commonly-understood {thing}
portions which the user should understand as placeholders for expected input. Since there's no leading $
, I figured the curly braces would be interpreted literally.
First attempt: curly braces
val name = "Michael"
println(s"Hi, $name! Please enter your birthday in the form \"{month} {day}\".")
IntelliJ IDEA gives me no reason to worry. Looks like a plain old string:
But I receive an error:
not found: value month
Second attempt: square braces
Fine, curly braces are a bad idea. What about square braces?
val name = "Michael"
println(s"Hi, $name! Please enter your birthday in the form \"[month] [day]\".")
Again, IDEA's formatting leads me to expect the braces will be interpreted literally.
But I receive a different error:
not found: type month
Third attempt: remove escaped quotes
Now I'm grasping at straws. I'll kill off the escaped quotes.
val name = "Michael"
println(s"Hi, $name! Please enter your birthday in the form {month} {day}.")
println(s"Hi, $name! Please enter your birthday in the form [month] [day].")
It works! Not formatted exactly as I would like, but enough to get the point across.
So, my question: why? Why does an escaped double-quote have any effect on the interpretation (and compilation) of these strings?
Obviously this won't derail my coding, but it is puzzling and counter-intuitive (to me). I'd love to understand what's happening behind the scenes.
it's a bug in scala - escape does not work with string interpolation.
it has nothing to do with braces. scala thinks, that string ends just after 1st escaped quote and can't find the following month
value