On 4/26/2013 1:59 PM, Diggory wrote:
The actual value shouldn't be taken into
account when determining which overload to call, only the type should matter,

D has an interesting feature called VRP (value range propagation), where implicit conversion very much depends on the value. For example:

ubyte b;
b = 100;    // ok
b = 300;    // error

This has been very successful in reducing the noise of having to insert casts all over the place.

For:

enum e = 1;

then e is a value that is known to be one. Hence VRP applies, and it can be implicitly cast to bool, ubyte, short, etc. For:

static e = 1;

the compiler cannot assume that e is always 1, hence e is treated as an int that could hold any value. Hence, it cannot be implicitly cast to bool, ubyte, short, etc.


> Ideally such an implicit conversion would only apply if it was the only possible valid implicit conversion,

This was how D originally worked. However, it produced a list of foolish errors, and a lot of complaints. The current scheme attempts to resolve ambiguities by doing a "partial ordering" (described elsewhere in this thread). This has been very successful.

To make use of overloading, there's no avoiding understanding this. A program that breaks due to different overloadings of:

    enum e = 1;
    static x = 1;

is misusing overloading just like one that breaks on different overloads of:

    enum e = 30000;
    static x = 40000;

I.e.:

    void foo(short) { ... back up my files ... }
    void foo(long) { ... launch nuclear missiles ... }

is a bad program.

Reply via email to