Add support implicit conversion between types

Simen Kjaeraas simen.kjaras at gmail.com
Sat Sep 7 05:56:44 PDT 2013


On 2013-09-06, 19:12, Dmitry Olshansky wrote:

> 06-Sep-2013 21:05, H. S. Teoh пишет:
>> 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. :-(
>
> I swear I've seen a pull request that enables it.

This one?

https://github.com/D-Programming-Language/dmd/pull/1356

It seems to be on its way.

-- 
   Simen


More information about the Digitalmars-d mailing list