The state of string interpolation

Paul Backus snarwin at gmail.com
Tue Dec 11 16:48:56 UTC 2018


On Tuesday, 11 December 2018 at 13:49:17 UTC, Sjoerd Nijboer 
wrote:
>
> I might be thinking something really silly, but since adding 
> such a thing as string interpolation is pretty much (wanted) 
> syntactic sugar, isn't it just possible to lower it? And if the 
> answer to this is yes, why can't we have some much wanted 
> syntactic library solutions as language features lowered in a 
> compiler pass?
> We can just maintain a separate set of lowerable language 
> constructs into a combination of existing library functions 
> instead of the true lowering of for loops and while loops and 
> everything the like.
> (we could lower a specific character before a string literal 
> into a literal![STRING]; )

It sounds like what you're asking for is a way to define rules 
for "lowering" one kind of syntax (e.g., string literals) into 
another (e.g., calls to library functions or templates).

This feature exists in other programming languages. The technical 
name for it is "AST macros." It's a very powerful feature, 
because it allows the programmer to essentially build their own 
custom programming languages on top of the "base" one provided by 
the compiler. For the same reason, it can also make code very 
difficult to read and understand. If any piece of syntax can be 
"lowered" into anything else, the only way to understand the code 
is to know what all the lowering rules are, and that can get 
difficult when every programmer is free to add to the pile.

Because of those potential difficulties, Walter has rejected 
every previous proposal to add AST macros to D, and there's no 
evidence to suggest that he's changed his mind since the last 
one. So, while it's true that AST macros would solve the issue 
with string interpolation, they're not a realistic option if we 
want our proposal to be accepted.


More information about the Digitalmars-d mailing list