String Interpolation

duckchess duckchess at chess.com
Wed Oct 25 19:15:42 UTC 2023


On Wednesday, 25 October 2023 at 15:05:52 UTC, Arafel wrote:
> On 25/10/23 13:29, Jonathan M Davis wrote:
>> IMHO, mixins should be explicit, obvious, and greppable, and 
>> adding a separate syntax for them which tries to hide them is 
>> going to make the language worse. However a string is 
>> generated, if the programmer wants to turn it into code, they 
>> should just use mixin, and if it's ugly, it's ugly, but at 
>> least it's clear what's going on.
>
> I agree, however the current options regarding string 
> interpolation seem to be suboptimal:
>
> 1. Get a string interpolation that doesn't get you strings, and 
> that needs support from the called function (or only work in 
> specific cases, not for `foo(string bar);`. You also can't 
> assign to string, nor use it in most libraries that don't 
> support it explicitly.
>
> 2. Use a library solution using string mixins (quite cumbersome 
> as a user, and probably also inefficient).
>
> 3. Introduce high level dependencies in the language itself, at 
> least to do the formatting.
>
> So I was just trying to brainstorm alternatives that would 
> allow a library solution.
>
> The biggest hurdle is accessing to the caller's scope, so far 
> only string mixins get that. There could be perhaps a different 
> feature that does it, i.e. a way to define a (compile time) 
> function that get access to the caller's scope, but I'm not 
> sure we would actually want that.

from the discussion in the chat: the things that is bad about 
mixins is that they allow mutations, so implicitly mixin 
something in can cause unexpected things to happen. i suggested 
const_mixin() instead that captures the scope, but only readonly.

so

```d
template i(string s) { mixin = s; }
float foo = 42;
writeln(i!"${foo}"); //ok
writeln(i!"${foo++}"); // error, cant mutate foo. need to use 
explicit mixin(i!"${foo++}")

```

it would be a more hygienic version of the idea, but no idea if 
walter would accept this.


More information about the Digitalmars-d mailing list