Re: [Vala] About method overloading, constructor overloading and extension methods



G add<G>(G a, G b)
{
    if (a is int && b is int)
        return (int) a + (int) b;
    if (a is double && b is double)
        return (double) a + (double) b;
    return 0;
}

int main(string[] args)
{
    var result = add(2, 3);
    result = add(1.2, 3.4);
    print("Result: %f\n".printf(result));
    return 0;
}


Results in:


 $ valac -C sample.vala
sample.vala:24.5-24.26: error: Assignment: Cannot convert from `double' to
`int'
    result = add(1.2, 3.4);
    ^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)


I think that will not work. Generics without any condition (where, extends,
implements) is very tricky. Maybe creating a wrapper type or one C function
for each signature/types used...



-- 
@geovanisouza92 - Geovani de Souza


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]