Add support implicit conversion between types
monarch_dodra
monarchdodra at gmail.com
Fri Sep 6 07:10:55 PDT 2013
On Friday, 6 September 2013 at 13:50:25 UTC, ilya-stromberg wrote:
> 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);
> }
Or just:
//----
import std.bigint;
T factorial(T)(T number)
{
return number <= 1 ?
T(1) :
number * factorial!T(number - 1);
}
//----
The problem though is that this requires "uniform construction",
which we don't have yet:
http://d.puremagic.com/issues/show_bug.cgi?id=9112
More information about the Digitalmars-d
mailing list