www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I convert string to expression somehow?

reply Godnyx <rempas tutanota.com> writes:
I'm trying to create a cool function that will let us do 
formatting sorter and faster. The function will work like that:

outln("My name is {name} and my age is {age}");

this will be equivalent to:

writeln("My name is ", name, " and my age is ", age);

or:

writefln("My name is %s and my age is %d", name, age);

You can see how much sorter and faster this is and how better it 
looks. This is actually the way Rust does it for anyone that 
happen to know. So I'm trying to find a way to convert the 
strings inside the curly braces so I can print them. In our 
example the function must do

write("My name is ");
write(name);
write("and my age is ");
write(age);

So yeah I want a way to convert the strings into expressions if 
that's possible. I also tried mixins but the variable cannot read 
at compile time so I'm out of luck...
Dec 12 2020
parent reply Tobias Pankrath <tobias+dlang pankrath.net> writes:
On Saturday, 12 December 2020 at 09:05:19 UTC, Godnyx wrote:
 I'm trying to create a cool function that will let us do 
 formatting sorter and faster. The function will work like that:

 outln("My name is {name} and my age is {age}");

 this will be equivalent to:

 writeln("My name is ", name, " and my age is ", age);

 or:

 writefln("My name is %s and my age is %d", name, age);
There was a DIP to bring something akin to this into the language, but there were also some decent counter proposals. See for example here: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_12_16.html
Dec 12 2020
parent Godnyx <rempas tutanota.com> writes:
On Saturday, 12 December 2020 at 11:03:06 UTC, Tobias Pankrath 
wrote:
 On Saturday, 12 December 2020 at 09:05:19 UTC, Godnyx wrote:
 I'm trying to create a cool function that will let us do 
 formatting sorter and faster. The function will work like that:

 outln("My name is {name} and my age is {age}");

 this will be equivalent to:

 writeln("My name is ", name, " and my age is ", age);

 or:

 writefln("My name is %s and my age is %d", name, age);
There was a DIP to bring something akin to this into the language, but there were also some decent counter proposals. See for example here: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_12_16.html
Thanks for your time man. I'm gonna check it out! Have a great day!
Dec 12 2020