Jump to page: 1 2
Thread overview
String interpolation
Nov 10, 2015
tired_eyes
Nov 10, 2015
wobbles
Nov 10, 2015
Tobias Pankrath
Nov 10, 2015
tired_eyes
Nov 10, 2015
wobbles
Nov 10, 2015
tcak
Nov 10, 2015
TheFlyingFiddle
Nov 10, 2015
tired_eyes
Nov 10, 2015
Márcio Martins
Nov 10, 2015
Andrea Fontana
May 20, 2020
mw
May 21, 2020
Paul Backus
May 21, 2020
mw
May 21, 2020
Paul Backus
May 21, 2020
Adam D. Ruppe
May 21, 2020
mw
May 21, 2020
Adam D. Ruppe
May 21, 2020
ZK
May 21, 2020
ZK
May 21, 2020
mw
November 10, 2015
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.
November 10, 2015
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...
November 10, 2015
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?
November 10, 2015
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:
???
November 10, 2015
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);
November 10, 2015
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.






November 10, 2015
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);
November 10, 2015
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.




November 10, 2015
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.
November 10, 2015
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

« First   ‹ Prev
1 2