Some Thoughts On String Interpolation [l10n, restricting access, AA]

Nick Treleaven nick at geany.org
Fri Oct 27 12:22:00 UTC 2023


On Thursday, 26 October 2023 at 11:18:46 UTC, kdevel wrote:
> **Accessing Elements Of An AA**
>
> ```d
> string[string] aa;
> aa["name"] = "value";
> writeln (i"The name in s is $(aa[\"name\"])"
> ```
>
> That typing is laborious. Isn't there a way to bind the 
> expression to the keys of the AA?

I think that is orthogonal to the DIP (though having to escape 
the `"` does make it worse). You can have a local reference in D:
```d
string[string] aa;
ref elem() => aa["name"];
aa["name"] = "value"; // insert the first key
writeln("The name in s is ", elem);
```
Note it's not possible to use `elem` to insert the key because 
`aa["name"]` without `=` is a lookup (not an insertion), even 
though it is being returned by ref. So `elem = "blah";` only 
works when the key already exists in `aa`.



More information about the Digitalmars-d mailing list