Just another example of missing string interpolation

Paul Backus snarwin at gmail.com
Fri Oct 20 15:50:20 UTC 2023


On Friday, 20 October 2023 at 15:29:29 UTC, Commander Zot wrote:
> why can't we do it with existing language features, and what 
> would be required to simply make ```foo(i!"cool ${var} 
> rox");``` work instead. because adding that feature might 
> benefit a lot more use cases.

Because templates are lexically scoped, the body of the `i` 
template would not be able to access `var` when expanding the 
string `"cool ${var} rox"`.

The only way in D for a template to access the scope where it's 
*used* instead of the scope where it's *defined* is to use a 
`mixin` (either string or template). So the best we can do with 
current language features is `foo(mixin(i!"cool ${var} rox"))`. 
(And indeed, there are libraries on code.dlang.org that do this.)

So, I guess the minimal language feature that would allow your 
example to work would be the ability to define a template whose 
result is implicitly mixed-in to the instantiating scope, without 
having to use the `mixin` keyword.

However, I doubt such a feature would be accepted. The whole 
point of requiring the `mixin` keyword is to signal to anyone 
reading the code that this is a place where D's normal scoping 
rules might be violated. Allowing that to happen implicitly would 
defeat the purpose of having a `mixin` keyword in the first place.


More information about the Digitalmars-d mailing list