String Interpolation

jmh530 john.michael.hall at gmail.com
Thu Oct 26 15:45:48 UTC 2023


On Thursday, 26 October 2023 at 10:40:34 UTC, Imperatorn wrote:
> On Thursday, 26 October 2023 at 10:12:49 UTC, Imperatorn wrote:
>> On Thursday, 26 October 2023 at 09:42:27 UTC, Arafel wrote:
>>> On 26/10/23 3:47, Adam D Ruppe wrote:
>>>> [...]
>>>
>>> You are not mentioning this part:
>>>
>>>> [...]
>>>
>>> I would find it perfect if there were an option to define 
>>> arbitrary prefixes to strings (other than the ones already 
>>> used by the language), like `i"Hello, ${name}` or `sql"SELECT 
>>> * FROM ${table};"`. Notice how the string itself doesn't need 
>>> any marker in Java:
>>>
>>>> [...]
>>>
>>> It's needing both what I don't like and find confusing: 
>>> `string s = i"Hello, ${world}".format` (or `.text`).
>>>
>>> We can do like Java, and assume that as soon as a template 
>>> expression is found, it will be automatically considered an 
>>> interpolated string, so actually strings literals will be 
>>> interpolated by default, and only if there are no expressions 
>>> the result would be a `string`.
>>
>> Yes, if D doesn't provide that. It would be the only language 
>> (created by man at least) that does not give the user a string 
>> if desired.
>
> Any DIP that doesn't provide a way to get a string from an 
> interpolated string is almost useless to the users. And the 
> language exists for the users. It doesn't exists for library 
> writers.

For reference, both Adam and Walter's versions show how it is 
possible with `.text` or `.format`.

https://github.com/dlang/dmd/pull/15722#issuecomment-1774153328
https://github.com/adamdruppe/interpolation-examples/blob/e275f110898758dbbbfaa5427a72c9a18387d065/01-basics.d#L13C25-L13C78

I'm sympathetic to not putting that functionality in the compiler.


More information about the Digitalmars-d mailing list