Method-call based on template argument

sclytrack sclytrack at iq87.fr
Wed May 9 04:12:27 PDT 2012


On 05/09/2012 12:33 PM, nrgyzer wrote:
> void main() {
>
>     int a = 10;
>     int b = 20;
>     std.stdio.writeln(calculate!("add")(a, b));
>     std.stdio.writeln(calculate!("subtract")(a, b));
>
> }



Below works
-----------

import std.stdio;

int calculate(string T)(int a, int b) if(T == "add")
{
    return a + b;
}

int calculate(string T)(int a, int b) if(T == "subtract")
{
    return a - b;
}

void main() {

    int a = 10;
    int b = 20;
    std.stdio.writeln(calculate!("add")(a, b));
    std.stdio.writeln(calculate!("subtract")(a, b));
}


Output
------
30
-10








More information about the Digitalmars-d-learn mailing list