String Interpolation
kdevel
kdevel at vogtner.de
Thu Oct 26 20:48:14 UTC 2023
On Thursday, 26 October 2023 at 16:42:23 UTC, Arafel wrote:
> [...]
> In D I'd like to do:
>
> ```d
> // Read config
> string basePath = readConfig("basePath");
> string inputFolder = readConfig("inputFolder");
> string outputFoler = readConfig("outputFolder");
> Item[] items = readConfig("itemList");
>
> // Process items
> foreach(item; items) {
> string inputFile =
> i"${basePath}/${inputFolder}/${item.name}.in";
> [...]
> ```
>
> I write this kind of code all the time...
Why? D has `buildPath` and `setExtension` [1]
```d
string inputFile = buildPath (basePath, inputFolder, item)
.setExtension ("in");
```
Okay, when `item` or `inputFolder` have leading path separators
the result might surprise...
[1] https://dlang.org/phobos/std_path.html
More information about the Digitalmars-d
mailing list