www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - String-Interpolation

reply Matthias Becker <Matthias_member pathlink.com> writes:
Some languages, mainly scripting-languages, have a feature called string
interpolation:

# $foo = "bla"
# echo "the value of foo is $foo."

So you can use variables in string literals. This is a cool feature. Perhaps it
would be nice to have it in D:

# SomeType instance = new SomeType();
# char[] = "The textrepresentation of instance is ${instance}."

This gets translated to:

# SomeType instance = new SomeType();
# char[] = "The textrepresentation of instance is " ~ (instance).toString() ~
"."

-- Matthias Bekcer
Dec 19 2004
parent reply teqDruid <me teqdruid.com> writes:
SomeType instance = new SomeType();
char[] str = format("the text representation of instance is %s.", instance);

I like the writef family.  BTW... I just got my
latest DDJ with Walter's "Printf Revisited"... good article Walter!

John

On Sun, 19 Dec 2004 13:57:51 +0000, Matthias Becker wrote:

 Some languages, mainly scripting-languages, have a feature called string
 interpolation:
 
 # $foo = "bla"
 # echo "the value of foo is $foo."
 
 So you can use variables in string literals. This is a cool feature. Perhaps it
 would be nice to have it in D:
 
 # SomeType instance = new SomeType();
 # char[] = "The textrepresentation of instance is ${instance}."
 
 This gets translated to:
 
 # SomeType instance = new SomeType();
 # char[] = "The textrepresentation of instance is " ~ (instance).toString() ~
 "."
 
 -- Matthias Bekcer

Dec 19 2004
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
teqDruid wrote:

 # SomeType instance = new SomeType();
 # char[] = "The textrepresentation of instance is " ~ (instance).toString() ~
"."

 SomeType instance = new SomeType();
 char[] str = format("the text representation of instance is %s.", instance);

Or you could do it yourself at runtime, if needed: import std.stdio; import std.string; char[] interpolate(char[] string, char[][char[]] vars) { foreach(char[] key; vars.keys) string = replace(string, "$" ~ key, vars[key]); return string; } void main() { char[][char[]] vars; vars["foo"] = "bla"; writefln( interpolate("the value of foo is $foo.", vars) ); } --anders PS. I'm sure D newcomers just *love* char[][char[]] ? Wouldn't string[string] be easier on the eyes... (ye olde "alias char[] string;" debate all over)
Dec 19 2004
prev sibling next sibling parent reply FEATURE <FEATURE_member pathlink.com> writes:
I agree with Matthias Becker.
My sugestion is to use '"Special string"'
Stats with '"
ends with "'
So empty string would be '""'
So we don't haveto change the behavior of "" strings.

First cool feature would be, that you can use " in there.
For example   write('"Then he said:"I have enaugh" and leaved."');
instead on    write("Then he said:\"I have enaugh\" and leaved.");
With \" the code seems to get ugly and not very readable. 

And there you could use $ also
I suggest:
--------------------
int a;
a=5;
write('"a equals $a; And I say to you:"Have a good day!" "');
--------------------------
Where ; ends the variable. without it is not very clear, where the variable
should end. The $ starts the variable. The $$ could represent the dollar.

I think it is very important to make the code easyer to read!

write("a equals "~toString(a)~" And I say to you:\"Have a good day!\" ");

What do you think?


In article <pan.2004.12.19.14.17.44.434407 teqdruid.com>, teqDruid says...
SomeType instance = new SomeType();
char[] str = format("the text representation of instance is %s.", instance);

I like the writef family.  BTW... I just got my
latest DDJ with Walter's "Printf Revisited"... good article Walter!

John

On Sun, 19 Dec 2004 13:57:51 +0000, Matthias Becker wrote:

 Some languages, mainly scripting-languages, have a feature called string
 interpolation:
 
 # $foo = "bla"
 # echo "the value of foo is $foo."
 
 So you can use variables in string literals. This is a cool feature. Perhaps it
 would be nice to have it in D:
 
 # SomeType instance = new SomeType();
 # char[] = "The textrepresentation of instance is ${instance}."
 
 This gets translated to:
 
 # SomeType instance = new SomeType();
 # char[] = "The textrepresentation of instance is " ~ (instance).toString() ~
 "."
 
 -- Matthias Bekcer


Dec 19 2004
parent Paul Bonser <misterpib gmail.com> writes:
FEATURE wrote:
 I agree with Matthias Becker.
 My sugestion is to use '"Special string"'
 Stats with '"
 ends with "'
 So empty string would be '""'
 So we don't haveto change the behavior of "" strings.
 
 First cool feature would be, that you can use " in there.
 For example   write('"Then he said:"I have enaugh" and leaved."');
 instead on    write("Then he said:\"I have enaugh\" and leaved.");
 With \" the code seems to get ugly and not very readable. 
 
 And there you could use $ also
 I suggest:
 --------------------
 int a;
 a=5;
 write('"a equals $a; And I say to you:"Have a good day!" "');
 --------------------------

How about using `s (backward single-quotes, on the ~ key)? It's a lot cleaner (and prettier) than using doubled quotes at each end. -PIB
Jan 14 2005
prev sibling parent reply Brad Anderson <brad dsource.dot.org> writes:
teqDruid wrote:
 I like the writef family.  BTW... I just got my
 latest DDJ with Walter's "Printf Revisited"... good article Walter!

Yes, it was a very good article. And did you notice on the cover, D is now one of the listed languages at the top? I'm not sure how long they've been doing that, but it was pretty cool to see. BA
Dec 19 2004
next sibling parent teqDruid <me teqdruid.com> writes:
On Sun, 19 Dec 2004 19:07:14 -0600, Brad Anderson wrote:

 teqDruid wrote:
 I like the writef family.  BTW... I just got my
 latest DDJ with Walter's "Printf Revisited"... good article Walter!

Yes, it was a very good article. And did you notice on the cover, D is now one of the listed languages at the top? I'm not sure how long they've been doing that, but it was pretty cool to see. BA

I hadn't noticed that, and I just went to check it. Awesome!
Dec 19 2004
prev sibling next sibling parent "Carlos Santander B." <csantander619 gmail.com> writes:
"Brad Anderson" <brad dsource.dot.org> escribió en el mensaje 
news:cq58l9$1tku$1 digitaldaemon.com...
| Yes, it was a very good article.  And did you notice on the cover, D is
| now one of the listed languages at the top?  I'm not sure how long
| they've been doing that, but it was pretty cool to see.
|
| BA

I'm not sure about that. It seems to change every month according to the
content 
of the issue.

-----------------------
Carlos Santander Bernal 
Dec 19 2004
prev sibling parent Paul Bonser <misterpib gmail.com> writes:
Brad Anderson wrote:
 teqDruid wrote:
 
 I like the writef family.  BTW... I just got my
 latest DDJ with Walter's "Printf Revisited"... good article Walter!

Yes, it was a very good article. And did you notice on the cover, D is now one of the listed languages at the top? I'm not sure how long they've been doing that, but it was pretty cool to see. BA

Dang, my copy hasn't come yet...or my subscription ran out...Hopefully just hasn't come :( -PIB
Jan 14 2005