May 20
On Tuesday, 10 November 2015 at 13:36:44 UTC, Andrea Fontana wrote:
> On Tuesday, 10 November 2015 at 12:40:07 UTC, Márcio Martins wrote:
>> writeln(interp!"The number #{a} is less than #{b}");
>>
>> Quite pleasant syntax this way :)
>> Not sure if it's feasible to do this on the language side.
>
> Yes. Here a (stupid!) proof of concept:
> http://dpaste.dzfl.pl/74b1a4e3c8c6

$ cat i.d
---------------------------------------------------------------------
import std.stdio;

template interp(X) {
  alias interp = mixin(interpGenCode!X);
}

void main() {
  int a = 10;
  int b = 20;
  writeln(mixin(interp!"The number #{a} is less than #{b}"));
  writeln(interp!"The number #{a} is less than #{b}");
}
---------------------------------------------------------------------
$ dmd.exe i.d
i.d(10): Error: template instance interp!"The number #{a} is less than #{b}" does not match template declaration interp(X)
i.d(11): Error: template instance interp!"The number #{a} is less than #{b}" does not match template declaration interp(X)

I'm wondering if this code has once worked? and seems dpaste.dzfl.pl no longer there.

Can we do string interpolation in D now?

Speaking of language evolution in the other thread,

C# has string interpolation now, here:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
string name = "Mark";
var date = DateTime.Now;
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");


Python have it here:
https://www.python.org/dev/peps/pep-0498/
>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."


How can we do it in D? or when will we have it :-)?

May 21
On Wednesday, 20 May 2020 at 23:41:15 UTC, mw wrote:
> Can we do string interpolation in D now?

There's an implementation in the dub package "scriptlike":

https://code.dlang.org/packages/scriptlike#string-interpolation
May 21
On Thursday, 21 May 2020 at 01:13:27 UTC, Paul Backus wrote:
> On Wednesday, 20 May 2020 at 23:41:15 UTC, mw wrote:
>> Can we do string interpolation in D now?
>
> There's an implementation in the dub package "scriptlike":
>
> https://code.dlang.org/packages/scriptlike#string-interpolation

Thank you! very nice:

// Output: The number 21 doubled is 42!
int num = 21;
writeln( mixin(interp!"The number ${num} doubled is ${num * 2}!") );

Is there any way to make it shorter? e.g.

writeln( s!"The number ${num} doubled is ${num * 2}!" );

i.e how to write this 's'?

May 21
On Thursday, 21 May 2020 at 06:57:28 UTC, mw wrote:
> On Thursday, 21 May 2020 at 01:13:27 UTC, Paul Backus wrote:
>> On Wednesday, 20 May 2020 at 23:41:15 UTC, mw wrote:
>>> Can we do string interpolation in D now?
>>
>> There's an implementation in the dub package "scriptlike":
>>
>> https://code.dlang.org/packages/scriptlike#string-interpolation
>
> Thank you! very nice:
>
> // Output: The number 21 doubled is 42!
> int num = 21;
> writeln( mixin(interp!"The number ${num} doubled is ${num * 2}!") );
>
> Is there any way to make it shorter? e.g.
>
> writeln( s!"The number ${num} doubled is ${num * 2}!" );
>
> i.e how to write this 's'?

Unfortunately, it's not possible. Without `mixin`, there's no way to give the template access to local variables like `num`.
May 21
On Thursday, 21 May 2020 at 06:57:28 UTC, mw wrote:
> i.e how to write this 's'?

gimme a like on the proposal to add to the language!

https://github.com/dlang/DIPs/pull/186

If accepted, that would let you write

i"stuff here".idup

to get an interpolated string.
May 21
On Thursday, 21 May 2020 at 12:53:50 UTC, Adam D. Ruppe wrote:
> On Thursday, 21 May 2020 at 06:57:28 UTC, mw wrote:
>> i.e how to write this 's'?
>
> gimme a like on the proposal to add to the language!
>
> https://github.com/dlang/DIPs/pull/186
>
> If accepted, that would let you write
>
> i"stuff here".idup
>
> to get an interpolated string.

Liked.

BTW, is the .idup must be there?
May 21
On Thursday, 21 May 2020 at 17:10:31 UTC, mw wrote:
> BTW, is the .idup must be there?

It is discussed more in the github document but basically the proposed built-in syntax returns a generic builder thing which can make more than just strings. The idup specifically asks it to make a copy into a string as opposed to the other things it can provide too.
May 21
On Thursday, 21 May 2020 at 17:17:49 UTC, Adam D. Ruppe wrote:
> On Thursday, 21 May 2020 at 17:10:31 UTC, mw wrote:
>> BTW, is the .idup must be there?
>
> It is discussed more in the github document but basically the proposed built-in syntax returns a generic builder thing which can make more than just strings. The idup specifically asks it to make a copy into a string as opposed to the other things it can provide too.

I love the extensibility of this, but perhaps there should be something like `ii"foo".idup` that's syntactic sugar for `i"foo".idup`. New users might get confused by the need for appending idup, and then they'll think about how other languages don't necessitate this perceived superfluity when using their interpolated string.
May 21
On Thursday, 21 May 2020 at 18:12:01 UTC, ZK wrote:
> I love the extensibility of this, but perhaps there should be something like `ii"foo".idup` that's syntactic sugar for `i"foo".idup`. New users might get confused by the need for appending idup, and then they'll think about how other languages don't necessitate this perceived superfluity when using their interpolated string.
Whoops! I meant to say `ii"foo"`, not `ii"foo".idup`.

May 21
On Thursday, 21 May 2020 at 06:57:28 UTC, mw wrote:
> On Thursday, 21 May 2020 at 01:13:27 UTC, Paul Backus wrote:
>> On Wednesday, 20 May 2020 at 23:41:15 UTC, mw wrote:
>>> Can we do string interpolation in D now?
>>
>> There's an implementation in the dub package "scriptlike":
>>
>> https://code.dlang.org/packages/scriptlike#string-interpolation
>
> Thank you! very nice:
>
> // Output: The number 21 doubled is 42!
> int num = 21;
> writeln( mixin(interp!"The number ${num} doubled is ${num * 2}!") );
>
> Is there any way to make it shorter? e.g.
>
> writeln( s!"The number ${num} doubled is ${num * 2}!" );
>
> i.e how to write this 's'?

And for debug print, I like the output be:

The number num=21 doubled is num * 2=42!

i.e print out the string of the original expression, like the stringy operator in C’s macro #var


Next ›   Last »
1 2