Thread overview | |||||
---|---|---|---|---|---|
|
December 12, 2020 Can I convert string to expression somehow? | ||||
---|---|---|---|---|
| ||||
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... |
December 12, 2020 Re: Can I convert string to expression somehow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Godnyx | 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 |
December 12, 2020 Re: Can I convert string to expression somehow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | 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!
|
Copyright © 1999-2021 by the D Language Foundation