Add support implicit conversion between types

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Fri Sep 6 06:21:42 PDT 2013


On Friday, 6 September 2013 at 13:01:14 UTC, Dicebot wrote:
> For example, use case that justifies operator overloading 
> (despite the danger) in my eyes is ability to replace built-in 
> types with custom ones. What is the similar rationale for 
> implicit conversion?

For exaple, for generic code:

T factorial(T)(T number)
{
    return number <= 1 ? 1 : number * factorial!T(number - 1);
}

void main()
{
    //works:
    factorial!int(5);

    //doesn't work:
    factorial!BigInt(5);
}

It can be critical for more complex cases, when you call one 
generic function from another one, like this:

unittest
{
    alias TypeTuple!(byte, ubyte, short, ushort, int, uint, long, 
ulong, BigInt) IntegralTypeList;

    foreach(T; IntegralTypeList)
    {
       assert(factorial!T(3) == 6);//Error: factorial (BigInt 
number) is not callable using argument types (int)
    }
}


More information about the Digitalmars-d mailing list