Add support implicit conversion between types

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Fri Sep 6 06:50:24 PDT 2013


On Friday, 6 September 2013 at 13:30:26 UTC, Dicebot wrote:
> So, what essentially is needed, is ability to implicitly 
> convert literals of built-in types to user types, not any 
> possible implicit conversion?
>
> I think allowing it with such restrictions can be reasonably 
> clean.

Yes, the ability to implicitly convert literals of built-in types 
to user types is REALLY needed.
The 2-nd error from "factorial" example

import std.bigint;

void main()
{
    assert(factorial!BigInt(BigInt(3)) == 6); //Error: 
incompatible types for ((1) : 
(number.opBinary(factorial(number.opBinary(1))))): 'int' and 
'BigInt'
}


The correct factorial implementation is:

T factorial(T)(T number)
{
    enum T one = 1;
    return number <= one ? one : number * factorial!T(number - 
one);
}

It's complicated, how do you think?


> not any possible implicit conversion?
I didn't think about yet. May be you are right, but I think it 
can be also useful. For example, if you can't change user-defined 
type.


More information about the Digitalmars-d mailing list