Re: Want to try out string interpolation in D?
On Monday, 23 October 2023 at 11:43:33 UTC, Imperatorn wrote: On Monday, 23 October 2023 at 11:12:20 UTC, matheus wrote: On Friday, 20 October 2023 at 16:41:40 UTC, Imperatorn wrote: [...] First of all thanks for writing this. Well this seems pretty nice... What DIP number is this? - I'd like to read why it was rejected. Matheus. Which one did you try? The first one is YAIDIP: https://github.com/John-Colvin/YAIDIP and the second one is DIP1027: https://github.com/dlang/DIPs/blob/master/DIPs/rejected/DIP1027.md Make sure you try them both Note: The YAIDIP is now superseed by the 1036e.
Re: Want to try out string interpolation in D?
On Monday, 23 October 2023 at 11:12:20 UTC, matheus wrote: On Friday, 20 October 2023 at 16:41:40 UTC, Imperatorn wrote: Here's a script to get you started ... Now try string interpolation: ```d import std.stdio; void main() { string name = "Johan"; int age = 37; int iq = 8001; int coffees = 1000; writeln(i"Your name is $name and you're $age years old"); writeln(i"You drink $coffees cups a day and it gives you $(coffees + iq) IQ"); } ``` Output: ``` Your name is Johan and you're 37 years old You drink 1000 cups a day and it gives you 9001 IQ ``` First of all thanks for writing this. Well this seems pretty nice... What DIP number is this? - I'd like to read why it was rejected. Matheus. Which one did you try? The first one is YAIDIP: https://github.com/John-Colvin/YAIDIP and the second one is DIP1027: https://github.com/dlang/DIPs/blob/master/DIPs/rejected/DIP1027.md Make sure you try them both
Re: Want to try out string interpolation in D?
On Friday, 20 October 2023 at 16:41:40 UTC, Imperatorn wrote: Here's a script to get you started ... Now try string interpolation: ```d import std.stdio; void main() { string name = "Johan"; int age = 37; int iq = 8001; int coffees = 1000; writeln(i"Your name is $name and you're $age years old"); writeln(i"You drink $coffees cups a day and it gives you $(coffees + iq) IQ"); } ``` Output: ``` Your name is Johan and you're 37 years old You drink 1000 cups a day and it gives you 9001 IQ ``` First of all thanks for writing this. Well this seems pretty nice... What DIP number is this? - I'd like to read why it was rejected. Matheus.
Re: Want to try out string interpolation in D?
On Friday, 20 October 2023 at 16:41:40 UTC, Imperatorn wrote: Here's a script to get you started Same instructions for DIP1027: ``` git clone https://github.com/WalterBright/dmd.git cd dmd git checkout dip1027 rdmd compiler/src/build.d ``` Then same instructions as above but without the need to copy interpolation.d
Re: Want to try out string interpolation in D?
On Friday, 20 October 2023 at 16:41:40 UTC, Imperatorn wrote: Here's a script to get you started ```bash git clone https://github.com/adamdruppe/dmd.git cd dmd git checkout interp rdmd compiler/src/build.d ``` [...] We just need support for it in the tools, then D will be a Dream come true
Want to try out string interpolation in D?
Here's a script to get you started ```bash git clone https://github.com/adamdruppe/dmd.git cd dmd git checkout interp rdmd compiler/src/build.d ``` I don't want to copy files anywhere, so the user has to do that manually: Copy dmd from the generated executable to your D bin installation folder Example: ``` copy generated\windows\release\64\dmd.exe C:\d\dmd2\windows\bin64 ``` Copy druntime/src/core/interpolation.d to your corresponding druntime folder + import Example: ``` copy druntime/src/core/interpolation.d C:\d\dmd2\src\druntime\import\core\ ``` Now try string interpolation: ```d import std.stdio; void main() { string name = "Johan"; int age = 37; int iq = 8001; int coffees = 1000; writeln(i"Your name is $name and you're $age years old"); writeln(i"You drink $coffees cups a day and it gives you $(coffees + iq) IQ"); } ``` Output: ``` Your name is Johan and you're 37 years old You drink 1000 cups a day and it gives you 9001 IQ ```
Re: String interpolation
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
Re: String interpolation
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.
Re: String interpolation
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`.
Re: String interpolation
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.
Re: String interpolation
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?
Re: String interpolation
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.
Re: String interpolation
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`.
String interpolation
https://forum.dlang.org/post/prlulfqvxrgrdzxot...@forum.dlang.org On Tuesday, 10 November 2015 at 11:22:56 UTC, wobbles wrote: int a = 1; int b = 4; writefln("The number %s is less than %s", a, b); writeln("The number ",a, " is less than ",b);
Re: String interpolation
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'?
Re: String interpolation
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
Re: String interpolation
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 :-)?
Re: String interpolation
On Wednesday, 6 January 2016 at 23:30:22 UTC, Guillaume Piolat wrote: Is there a fancy way to have some kind of string interpolation in D? This is probably the best you're going to be able to do in D in regards to string interpolation. https://github.com/Abscissa/scriptlike#string-interpolation
Re: String interpolation
On Wednesday, 6 January 2016 at 23:30:22 UTC, Guillaume Piolat wrote: Is there a fancy way to have some kind of string interpolation in D? Other than std.format.format that is?
String interpolation
Is there a fancy way to have some kind of string interpolation in D?
Re: String interpolation
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
Re: String interpolation
On Tuesday, 10 November 2015 at 11:29:32 UTC, TheFlyingFiddle wrote: On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1, b = 4; writefln("The number %s is less than %s", a, b); You can't do it the ruby / perl / php way in D. It could be possible if we had AST macros in the language but as it stands now it's not possible to do that. the closest you could get is something like this: string s = aliasFormat!("The number $a is less than $b", a, b); or aliasWrite!("The number $a is less than $b", a, b); Not really recommended though as these would end up creating lots of template bloat.! You could perhaps do it with a mixin instead of AST macros: int a = 10; int b = 20; writeln(mixin(interp!"The number #{a} is less than #{b}")); One thing that would make this a lot more elegant would be the ability to instantiate mixins in templates. For example: template interp(X) { alias interp = mixin(interpGenCode!X); } 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.
Re: String interpolation
On Tuesday, 10 November 2015 at 11:29:32 UTC, TheFlyingFiddle wrote: On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1, b = 4; writefln("The number %s is less than %s", a, b); You can't do it the ruby / perl / php way in D. It could be possible if we had AST macros in the language but as it stands now it's not possible to do that. the closest you could get is something like this: string s = aliasFormat!("The number $a is less than $b", a, b); or aliasWrite!("The number $a is less than $b", a, b); Not really recommended though as these would end up creating lots of template bloat. Thank everybody for the answers. I know about writefln, but I was hoping I just overlooked some simpler way.
Re: String interpolation
On Tuesday, 10 November 2015 at 11:22:56 UTC, wobbles wrote: On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: [...] std.string.format and std.format are the standard options. What are you missing? Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1; int b = 4; writefln("The number %s is less than %s", a, b); D: int a = 1; int b = 4; writeln("The number ", a, " is less than ", b);
Re: String interpolation
On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1, b = 4; writefln("The number %s is less than %s", a, b); You can't do it the ruby / perl / php way in D. It could be possible if we had AST macros in the language but as it stands now it's not possible to do that. the closest you could get is something like this: string s = aliasFormat!("The number $a is less than $b", a, b); or aliasWrite!("The number $a is less than $b", a, b); Not really recommended though as these would end up creating lots of template bloat.
Re: String interpolation
On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: [...] std.string.format and std.format are the standard options. What are you missing? Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1; int b = 4; writefln("The number %s is less than %s", a, b);
Re: String interpolation
On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything. std.string.format and std.format are the standard options. What are you missing? Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ???
Re: String interpolation
On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything. std.string.format and std.format are the standard options. What are you missing?
Re: String interpolation
On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything. Whats clumsy about it? If it's the style (excessive use of UFCS), I'd have to agree with you. Could easily have been written as: format("Mary had a %s lamb", "little"); which I personally think is more readable...
String interpolation
Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything.