Thread overview
Using double value in string template mixin
Feb 26, 2016
Dibyendu Majumdar
Feb 26, 2016
BBasile
Feb 26, 2016
Dibyendu Majumdar
Feb 26, 2016
BBasile
Feb 26, 2016
BBasile
Feb 26, 2016
Dibyendu Majumdar
February 26, 2016
Hi,

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

Thanks and Regards
Dibyendu
February 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
> Dibyendu

Have you an example of what's failing right now to show ?
February 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:
>> 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'
February 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.



February 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.
February 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!