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