Closed LPeter1997 closed 2 years ago
This introduces a new escape sequence,
\$
, but that shouldn't be an issue.
Which, practically, doesn't ever have to be escaped because we don't write ${
or $a
(unless interoplate), whereas in all other cases, like $1000
or $ 1000
or etc. it will be interpreted as $
character
@WhiteBlackGoose That is true, but this is important for the same reason as \'
being '
in string literals, simpler metaprogramming and code generation. It's simpler to have a single function in our standard library that "stupidly" escapes all characters that are allowed to be escaped than to start differentiating in different contexts.
Note: This proposal doesn't intend to solve multiline string literals. This is purely for one-liner strings.
Introduction
String interpolation allows placeholders to be added to string literals that get filled in by stringified expressions. It's generally faster than raw string concatenation in languages with immutable strings, as the compiler usually turns interpolated strings into an expression using a more efficient builder type (something like
StringBuilder
in C#).For example, in C#
will be evaluated to the string
"1 + 2 = 3"
and the compiler underneath could have generated something like:(Note, that it's probably not exactly this in C#)
Syntax variations
This is one of the few features that has relatively few syntax variations among mainstream languages. Main variables between syntaxes:
$
in C#,f
in Python, interpolated by default in Kotlin)$
in Scala, Kotlin){}
in C#)${}
in Scala, Kotlin)To showcase how this looks like in different languages, we'll assume that a local variable named
x
is defined, and we want to write the equivalent interpolated string tox.ToString() + " + 1 = " + (x + 1).ToString()
.$"{x} + 1 = {x + 1}"
(escaping{}
can be done by duplicating it$"{{}}" == "{}"
)"$x + 1 = ${x + 1}"
(escaping$
can be done with\$
)"$x + 1 = $(x + 1)"
(escaping$
can be done with\$
)f"{x} + 1 = {x + 1}"
(escaping{}
can be done by duplicating itf"{{}}" == "{}"
)"#{x} + 1 = #{x + 1}"
(escaping#
can be done with\#
)Proposal for Fresh
I believe there is no reason not to have string interpolation enabled by default, so I'd say there's no need for a prefix character, like
$
in C#. For the interpolation syntax, the one used by Kotlin (and many other languages) seems the cleanest to me, so I suggest that. This introduces a new escape sequence,\$
, but that shouldn't be an issue.Possible conflict in language features
Currently #41 suggest every nonstatic member has to be prefixed with
this
. This means, that interpolating members has to be with the more verbose,${...}
syntax (otherwise, something like$this.x
would be equivalent tothis.ToString() + ".x"
). For now, I don't think this is such a big issue, but when making the final decision, we might decide to use a different syntax because of this.