proposal: a new string litteral to embed variables in a string

Timothee Cour thelastmammoth at gmail.com
Thu Oct 31 14:24:29 PDT 2013


the proposed new syntax
r{var1=@a; and var2=@foo}

is replaced by a tuple:
("var1=", a, "; and var2=", foo)
where @ denotes escaping symbols.

@ itself be escaped with \@.

optionally, expressions can be incorporated:
r{a2=@(a*2); and fooU=@(foo.toUpper)}

I've actually already implemented this feature via a mixin, and find it
extremely useful, but removing the mixing via this proposal would make it
even more palatable. It works using a simple grammar that searches for
valid identifiers after a @, or finds nested expressions nested inside
parenthesis (arbitrary nesting allowed).

Proper tooling will syntax highlight correctly the nested variables.


This feature is especially useful when there are a few variables involved,
as alternatives are clunky


use cases:
A) simple string formatting (eg: formattedWrite)
----
q{first var=@a, second=@b, third=@c!}.foo
vs:
("first var=",a," second=",b," third=",c,"!").foo // `",,"` for a single `@`
("first var=%s, second=%s, third=%s!", a, b, c).foo => // error prone esp
with many variables
or alternatives involving ~ :
("first var="~a.to!string~", second="~b.to!string~",
third="~c.to!string~"!").foo
//clunky

B) parsing (eg formattedRead)
same advantages as above
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20131031/bdb26650/attachment.html>


More information about the Digitalmars-d mailing list