Add support implicit conversion between types

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 6 10:13:37 PDT 2013


On Fri, Sep 06, 2013 at 10:05:47AM -0700, H. S. Teoh wrote:
> On Fri, Sep 06, 2013 at 05:14:48PM +0200, ilya-stromberg wrote:
> > On Friday, 6 September 2013 at 14:26:17 UTC, H. S. Teoh wrote:
> > >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?
> > 
> > Yes, but it isn't always possible.
> > 
> > >>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)) ?
> > 
> > No, I have the error:
> > Error: function expected before (), not byte of type byte
> > Error: function expected before (), not ubyte of type ubyte
> > Error: function expected before (), not short of type short
> > Error: function expected before (), not ushort of type ushort
> > Error: function expected before (), not int of type int
> > Error: function expected before (), not uint of type uint
> > Error: function expected before (), not long of type long
> > Error: function expected before (), not ulong of type ulong
> > 
> > As monarch_dodra pointed above, we haven't got "uniform
> > construction" support.
> 
> Hmm, I see. This is an unfortunate limitation. In C++, writing int(123)
> actually works. Looks like D is lacking in this area. :-(
[...]

Hmm, could this be a possible (though somewhat ugly) workaround?

	foreach (T; IntegralTypeList)
	{
		assert(factorial(to!T(3) == 6));
	}

AFAIK, if T==int, then std.conv.to should simply alias itself away. And
it *should* be able to handle ctors that take the requisite type, I
think.


T

-- 
"The number you have dialed is imaginary. Please rotate your phone 90 degrees and try again."


More information about the Digitalmars-d mailing list