digitalmars.D.learn - Using double value in string template mixin
- Dibyendu Majumdar (5/5) Feb 26 2016 Hi,
- BBasile (3/8) Feb 26 2016 Have you an example of what's failing right now to show ?
- Dibyendu Majumdar (10/15) Feb 26 2016 I am trying something like this:
- BBasile (12/16) Feb 26 2016 No you cannot because you would have to convert the values to
- BBasile (15/19) Feb 26 2016 Erratum! Actually you can, example:
- Dibyendu Majumdar (2/16) Feb 26 2016 Thank you!
Hi, How do I use a double value in a mixin template that is generating string? Thanks and Regards Dibyendu
Feb 26 2016
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 DibyenduHave you an example of what's failing right now to show ?
Feb 26 2016
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: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'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 ?
Feb 26 2016
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
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
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