www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using double value in string template mixin

reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
Hi,

How do I use a double value in a mixin template that is 
generating string?

Thanks and Regards
Dibyendu
Feb 26 2016
parent reply BBasile <b2.temp gmx.com> writes:
On Friday, 26 February 2016 at 11:03:43 UTC, Dibyendu Majumdar 
wrote:
 Hi,

 How do I use a double value in a mixin template that is 
 generating string?

 Thanks and Regards
 Dibyendu
Have you an example of what's failing right now to show ?
Feb 26 2016
parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Friday, 26 February 2016 at 11:07:28 UTC, BBasile wrote:
 On Friday, 26 February 2016 at 11:03:43 UTC, Dibyendu Majumdar 
 wrote:
 How do I use a double value in a mixin template that is 
 generating string?
Have you an example of what's failing right now to show ?
I am trying something like this: template MyTAlloc(int n_vars, double v) { const char[] MyT = "MyT_init(cast(MyT *) alloca(alloc_size(" ~ n_vars ~ ")), " ~ n_vars ~ ", " ~ v ~ ")"; } MyT *t = mixin(MyTAlloc!(2,5.0)); Error: incompatible types for (("MyT_init(cast(MyT *) alloca(alloc_size(" ~ cast(immutable(char))2 ~ ")), " ~ cast(immutable(char))2 ~ ", ") ~ (5.00000)): 'string' and 'double'
Feb 26 2016
parent reply BBasile <b2.temp gmx.com> writes:
On Friday, 26 February 2016 at 11:13:08 UTC, Dibyendu Majumdar 
wrote:
 I am trying something like this:

 template MyTAlloc(int n_vars, double v) {
 const char[] MyT = "MyT_init(cast(MyT *) alloca(alloc_size(" ~ 
 n_vars ~ ")), " ~ n_vars ~ ", " ~ v ~ ")";
No you cannot because you would have to convert the values to string using std.conv.to or std.format.format(), but they don't work at compile time (see https://issues.dlang.org/show_bug.cgi?id=13568). Ideally like this: template MyTAlloc(int n_vars, double v) { const char[] MyT = "MyT_init(cast(MyT *) alloca(alloc_size(" ~ to!string(n_vars) ~ ")), " ~ to!string(n_vars) ~ ", " ~ to!string(v) ~ ")"; But you can try with regular templates or mixin templates.
Feb 26 2016
parent reply BBasile <b2.temp gmx.com> writes:
On Friday, 26 February 2016 at 11:26:51 UTC, BBasile wrote:
 No you cannot because you would have to convert the values to 
 string using std.conv.to or std.format.format(), but they don't 
 work at compile time (see 
 https://issues.dlang.org/show_bug.cgi?id=13568).
Erratum! Actually you can, example: import std.stdio; string foo(double a)() { return "auto value = " ~ a.stringof ~ ";"; } void main(string[] args) { mixin(foo!0.1); writeln(value); // 0.1 writeln(typeof(value).stringof); // double } So you have to use .stringof on the template argument. Sorry for the previous answer.
Feb 26 2016
parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Friday, 26 February 2016 at 11:37:32 UTC, BBasile wrote:
 Erratum! Actually you can, example:

 import std.stdio;

 string foo(double a)()
 {
     return "auto value = " ~ a.stringof ~ ";";
 }

 void main(string[] args)
 {
     mixin(foo!0.1);
     writeln(value); // 0.1
     writeln(typeof(value).stringof); // double
 }

 So you have to use .stringof on the template argument.
 Sorry for the previous answer.
Thank you!
Feb 26 2016