Add support implicit conversion between types

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 6 15:44:08 PDT 2013


On Fri, Sep 06, 2013 at 07:25:21PM +0200, ilya-stromberg wrote:
> On Friday, 6 September 2013 at 17:15:03 UTC, H. S. Teoh wrote:
> >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.
> 
> I use:
> 
>  	foreach (T; IntegralTypeList)
>  	{
>  		assert(factorial(cast(T)3) == 6));
>  	}
> 
> I works, but looks a little strange.
> I belive that compiler should optimise this (do nothing), but I am
> not sure.

I checked the disassembly for cast(T)5 for T==int and T==BigInt. Seems
the compiler is doing the "right thing" in both cases. For T==int, the
compiler just loads 5 directly into the register; for T==BigInt, it
calls the BigInt ctor with 5 as parameter to construct an instance of
BigInt.

So, this seems to work, even though it looks ugly.


T

-- 
A linguistics professor was lecturing to his class one day. "In
English," he said, "A double negative forms a positive. In some
languages, though, such as Russian, a double negative is still a
negative. However, there is no language wherein a double positive can
form a negative." A voice from the back of the room piped up, "Yeah,
yeah."


More information about the Digitalmars-d mailing list