Hacker News new | ask | show | jobs
by sqrt17 2756 days ago
Javascript has `I have ${n} apples`, or even `This is chapter ${i+1}.`

In terms of interpolation, that's the variant that I like best.

4 comments

Perl 6 does it without the sigil outside the bracket :)

"I have {some-complex-calc($i)} frobitzes"

Unlike Python's `"I have {} apples".format(n)` and various shells' `"I have $n apples"`, Javascript's and Apache Groovy's `"This is chapter ${i+1}"` mean the lexer must make a call to the parser when lexing the string contents -- this allows `"This is ${"chapter"} ${i+1}"` to parse. Don't know about Javascript, but this makes Groovy's parser unreadable, and stuck on a very old version of Antlr, i.e. 2 instead of 4 (the latest).
In P6:

grammar one-grammar { rule { <some-pattern> <another-grammar> .... } }

Can't get simpler. Can't be more readable.

AFAIK, Groovy 3 is targeting Antlr 4.
Also in groovy. I too am a fan.