Thread overview
String-Interpolation
Dec 19, 2004
Matthias Becker
Dec 19, 2004
teqDruid
Dec 19, 2004
FEATURE
Jan 15, 2005
Paul Bonser
Dec 20, 2004
Brad Anderson
Dec 20, 2004
teqDruid
Jan 15, 2005
Paul Bonser
December 19, 2004
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


December 19, 2004
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

December 19, 2004
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)
December 19, 2004
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
>


December 20, 2004
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
December 20, 2004
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!
December 20, 2004
"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


January 15, 2005
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
January 15, 2005
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