Add support implicit conversion between types

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 6 07:24:50 PDT 2013


On Fri, Sep 06, 2013 at 03:21:42PM +0200, ilya-stromberg wrote:
> 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);
> }

I thought the usual D idiom was to write factorial(5) and
factorial(BigInt(5)) and let the compiler figure out which template
instance you wanted?


> 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)

You could just write factorial(T(3)) ?


T

-- 
Prosperity breeds contempt, and poverty breeds consent. -- Suck.com


More information about the Digitalmars-d mailing list