String interpolation variables

165 views
Skip to first unread message

Jad

unread,
Sep 15, 2017, 7:41:34 PM9/15/17
to Project Lombok
Hi. Maybe it's possible to support string interpolation variables like in groovy or scala or kotlin: "My var1=${var1}, and var2=${var2}"? If no, it would be good functionality.

Marco Servetto

unread,
Sep 15, 2017, 7:58:00 PM9/15/17
to project...@googlegroups.com
I'm not sure what is the advantage of such syntax:

String res="hi "+x+"!";//pure java
String res="hi ${x}!";//proposed syntax saves a single char

I sort of understand the
String res="hi {x}!";//swift syntax saves two char
but it has the price that now { and } need to be escaped....
> --
> You received this message because you are subscribed to the Google Groups
> "Project Lombok" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to project-lombo...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Martin Grajcar

unread,
Sep 15, 2017, 10:11:16 PM9/15/17
to project...@googlegroups.com
On Sat, Sep 16, 2017 at 1:57 AM, Marco Servetto <marco.s...@gmail.com> wrote:
I'm not sure what is the advantage of such syntax:

String res="hi "+x+"!";//pure java
String res="hi ${x}!";//proposed syntax saves a single char

When you format "pure java" according to Sun conventions, you get four more characters. No big difference, but interpolation is IMHO more readable.
 
I sort of understand the
String res="hi {x}!";//swift syntax saves two char
but it has the price that now { and } need to be escaped....

There's always at least one character which needs escaping, be it "$" or "{". Escaping of the closing brace is unnecessary as there's nothing to interpolate in "}x}y", but it might be required for symmetry reasons.

Lone "$" is pretty common, with optional braces allowing to embed complex expression or use a variable immediately followed by characters allowed in identifiers, like in Perl or Kotlin:
"My name is $firstname $surname and it's my ${age}th birthday"
instead of
"My name is " + firstname + surname + " and it's my " + age + "th birthday"

So when compared to standardly formatted java, you save up to seven characters per interpolation. No big deal, but it also looks better and is much easier to edit.
 

On 13 September 2017 at 02:24, Jad <jad...@gmail.com> wrote:
> Hi. Maybe it's possible to support string interpolation variables like in
> groovy or scala or kotlin: "My var1=${var1}, and var2=${var2}"? If no, it
> would be good functionality.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Project Lombok" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages