April 15, 2017 Interpolated strings | ||||
---|---|---|---|---|
| ||||
Hi all I've been wanting to have support for interpolated strings in D for some time now that will allow you to write e.g.: auto a = 7; writeln( $"{a} times 3 is {a*3}" ); Code speaks louder that words so I've made a PR that adds this support to ddmd as a RFC [1]. The compiler will basically lower the $"..." string to a mixin that concatenates the expression parts of the (inside the {}) and the plain text parts. I do recognize that this is not part of the 2017 H1 plan but I also believe such smaller improvements added regularly can make the language both more productive and attractive. In case this addition gets a thumbs up I will of course improve test coverage and any needed quality of implementation. [1] https://github.com/dlang/dmd/pull/6703 |
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote:
> Hi all
>
> I've been wanting to have support for interpolated strings in D for some time now that will allow you to write e.g.:
>
> auto a = 7;
> writeln( $"{a} times 3 is {a*3}" );
>
> Code speaks louder that words so I've made a PR that adds this support to ddmd as a RFC [1].
>
> The compiler will basically lower the $"..." string to a mixin that concatenates
> the expression parts of the (inside the {}) and the plain text parts.
>
> I do recognize that this is not part of the 2017 H1 plan but I also believe such smaller improvements added regularly can make the language both more productive and attractive.
>
> In case this addition gets a thumbs up I will of course improve test coverage and any needed quality of implementation.
>
> [1] https://github.com/dlang/dmd/pull/6703
This has been proposed before, and I still don't see the added value compared to:
auto a=7;
writeln(a, " times 3 is ", a*3);
besides adding compiler complexity, language complexity and parsing complexity which leaves room for more bugs to be invented. I'm a bit more harsh than I could be, but if this simple question has no clear answer I don't see why it sould make it into the language.
|
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Saturday, 15 April 2017 at 20:12:41 UTC, cym13 wrote: > On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote: > This has been proposed before, and I still don't see the added value compared to: > > auto a=7; > writeln(a, " times 3 is ", a*3); > > besides adding compiler complexity, language complexity and parsing complexity which leaves room for more bugs to be invented. I'm a bit more harsh than I could be, but if this simple question has no clear answer I don't see why it sould make it into the language. Try a different context: auto a = 7; import std.format; auto str = format("%s times 3 is %s", a, a*3); //or import std.conv; auto str = text(a, " times 3 is ", a*3); //or auto str = $"{a} times 3 is {a*3}"; |
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote: > The compiler will basically lower the $"..." string to a mixin that concatenates > the expression parts of the (inside the {}) and the plain text parts. It's easy implementable as a library (see https://github.com/Abscissa/scriptlike#string-interpolation) so it does not seem like a good idea to modify the language, only to change interp!"" to $"". |
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Saturday, 15 April 2017 at 20:20:49 UTC, Stanislav Blinov wrote:
> On Saturday, 15 April 2017 at 20:12:41 UTC, cym13 wrote:
>> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote:
>
>> This has been proposed before, and I still don't see the added value compared to:
>>
>> auto a=7;
>> writeln(a, " times 3 is ", a*3);
>>
>> besides adding compiler complexity, language complexity and parsing complexity which leaves room for more bugs to be invented. I'm a bit more harsh than I could be, but if this simple question has no clear answer I don't see why it sould make it into the language.
>
> Try a different context:
>
> auto a = 7;
>
> import std.format;
> auto str = format("%s times 3 is %s", a, a*3);
>
> //or
>
> import std.conv;
> auto str = text(a, " times 3 is ", a*3);
>
> //or
>
> auto str = $"{a} times 3 is {a*3}";
Yes this is a very basic lowering. The value comes from the fact (assumption?) that this pattern of constructing a string from a mix of strings and expressions is very common. The value of a feature could be described as
usage * productivity_improvement = feature_value
So while the productivity_improvement may be modest the usage is high enough to give a high feature_value and justify the addition. A sign of this is the number of other popular languages supporting this feature (not arguing for just copy other languages but it is still an indicator). Unfortunately I have no hard data for these numbers.
|
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to crimaniak | On Saturday, 15 April 2017 at 20:35:56 UTC, crimaniak wrote:
> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote:
>> The compiler will basically lower the $"..." string to a mixin that concatenates
>> the expression parts of the (inside the {}) and the plain text parts.
> It's easy implementable as a library (see https://github.com/Abscissa/scriptlike#string-interpolation) so it does not seem like a good idea to modify the language, only to change interp!"" to $"".
Lowerings are by definition possible to implement in a library.
Using that argument foreach (i; 0..100) {} should not be part of the
language even though I think very few would live without foreach.
|
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote: > ... First, there's a process for language additions, please see https://github.com/dlang/DIPs/blob/master/README.md Secondly, I can tell you that any proposal that can be solved via the standard library has a very low chance of being accepted. D is already a complex language and it would take a important problem in order to justify making it more complex. As this is already do-able via Phobos, I would personally vote no to this addition. |
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote: > Hi all > > <snip> I shared my thoughts on such a feature just a couple weeks ago: https://forum.dlang.org/post/oedeijdewmhazaqazdyo@forum.dlang.org |
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | On Saturday, 15 April 2017 at 20:57:33 UTC, Jack Stouffer wrote:
> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote:
>> ...
>
> First, there's a process for language additions, please see https://github.com/dlang/DIPs/blob/master/README.md
>
> Secondly, I can tell you that any proposal that can be solved via the standard library has a very low chance of being accepted. D is already a complex language and it would take a important problem in order to justify making it more complex.
>
> As this is already do-able via Phobos, I would personally vote no to this addition.
Thank you.
I have been following the forums for many years so am aware of the
process and chances of something like this getting in.
For that exact reason I wanted to do a minimal implementation to show that I'm serious
and a RFC. Doing a DIP is a lot of work that I'm only willing to use my time on if
I have some confidence that it will not be in vain. I will do the DIP if this ever
gets a thumbs up.
|
April 15, 2017 Re: Interpolated strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | On Saturday, 15 April 2017 at 21:03:27 UTC, Xinok wrote:
> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen wrote:
>> Hi all
>>
>> <snip>
>
> I shared my thoughts on such a feature just a couple weeks ago:
>
> https://forum.dlang.org/post/oedeijdewmhazaqazdyo@forum.dlang.org
Most of you points applies to std.conv.text as well don't they?
If you need special output buffers or memory management then use the tools right for that. This is about making the common case easier and keep the uncommon as it has always been.
|
Copyright © 1999-2021 by the D Language Foundation