Re: Template variable not reach at compile

2018-07-21 Thread Greatsam4sure via Digitalmars-d-learn

On Saturday, 21 July 2018 at 13:13:11 UTC, Mike Parker wrote:

On Saturday, 21 July 2018 at 12:17:54 UTC, Greatsam4sure wrote:

Sorry for the typo.  This is the problem

auto arithmetic(T, V, U)(T a,  V b,  U op){
   return mixin("a"~op~"b");
}

//call like this
arithmetic(1.5,2.5,"+");


Compiler says the variable op is not reach at compile time.  
So how can the varible a and b be reach at compile time and op 
is not reach. I will appreciate any help. I have also use a 
static if but the same complain.  Just a newbie in D


You aren't using a or b at compile time -- you're using the 
string literals "a" and "b", which have nothing at all to do 
with a and b. In order to get what you want, you need to make 
op a template parameter like this:


auto arithmetic(string op, T, V)(T a, V b)
{
mixin("a"~op~"b");
}

arithmetic!("+")(1.5, 2.5);



Thanks Sir.  Really appreciate your reply it works for me. I also 
want to thank you for all your labour in the dlang ecosystem or 
foundation. I read about the dlang announce group every day. I 
will also want update to dlang this week or close it down or 
redirect to the announce group directly




Re: Template variable not reach at compile

2018-07-21 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 21 July 2018 at 12:17:54 UTC, Greatsam4sure wrote:

Sorry for the typo.  This is the problem

auto arithmetic(T, V, U)(T a,  V b,  U op){
   return mixin("a"~op~"b");
}

//call like this
arithmetic(1.5,2.5,"+");


Compiler says the variable op is not reach at compile time.  So 
how can the varible a and b be reach at compile time and op is 
not reach. I will appreciate any help. I have also use a static 
if but the same complain.  Just a newbie in D


You aren't using a or b at compile time -- you're using the 
string literals "a" and "b", which have nothing at all to do with 
a and b. In order to get what you want, you need to make op a 
template parameter like this:


auto arithmetic(string op, T, V)(T a, V b)
{
mixin("a"~op~"b");
}

arithmetic!("+")(1.5, 2.5);



Re: Template variable not reach at compile

2018-07-21 Thread rikki cattermole via Digitalmars-d-learn

On 22/07/2018 12:15 AM, Greatsam4sure wrote:

auto arithmetic(T, V, U)(T a,  V b,  U op){
    return mixin("a"~op~"b");
}

//call like this
arithmetic(1.5,2.5,"op");


Compiler says the variable op is not reach at compile time.  So how can 
the varible a and b be reach at compile time and op is not reach. I will 
appreciate any help. I have also use a static if but the same complain.  
Just a newbie in D


Simple, it was never requested to be reached.

The correct code is:

auto arithmetic(string op, T, V)(T a, V b) {
return mixin("a" ~ op ~ "b");
}

arithmetic!("op")(1.5, 2.5);

D is not dynamic, if you need access to compile time features like 
string mixin, you must pass them via the template parameters. You can't 
use regular old variables to do this, as the code hasn't even been 
generated yet.


Template variable not reach at compile

2018-07-21 Thread Greatsam4sure via Digitalmars-d-learn

Sorry for the typo.  This is the problem

auto arithmetic(T, V, U)(T a,  V b,  U op){
   return mixin("a"~op~"b");
}

//call like this
arithmetic(1.5,2.5,"+");


Compiler says the variable op is not reach at compile time.  So 
how can the varible a and b be reach at compile time and op is 
not reach. I will appreciate any help. I have also use a static 
if but the same complain.  Just a newbie in D


Template variable not reach at compile

2018-07-21 Thread Greatsam4sure via Digitalmars-d-learn

auto arithmetic(T, V, U)(T a,  V b,  U op){
   return mixin("a"~op~"b");
}

//call like this
arithmetic(1.5,2.5,"op");


Compiler says the variable op is not reach at compile time.  So 
how can the varible a and b be reach at compile time and op is 
not reach. I will appreciate any help. I have also use a static 
if but the same complain.  Just a newbie in D