[Issue 19399] Different Conversion Rules for Same Value and Type -- Enum

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 26 08:17:11 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=19399

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com

--- Comment #8 from Walter Bright <bugzilla at digitalmars.com> ---
Here's how function overload resolution works:

    f(ubyte);
    f(ushort);

    enum E : ushort { a = 10; }

    f(E.a); // which is called?

There are 4 matching levels, 1 is worst and 4 is best:

1. no match
2. implicit conversion
3. conversion to const
4. exact match

E.a can be implicitly converted to a ubyte.
E.a can also be implicitly converted to ushort.

Both are match level 2. To disambiguate this, we now move to "partial
ordering". For partial ordering, we ignore what the argument is. We only look
at the function parameters. We ask the question "can the parameters of f(ubyte)
be used to call the f(ushort)?" The answer is yes, because ubyte can be
implicitly converted to ushort. Then we ask "can the parameters of f(ushort) be
used to call f(ubyte)?" The answer is no, because ushort cannot be implicitly
converted to ubyte.

Therefore, f(ubyte) is "more specialized", and the more specialized function is
selected.

Partial matching is a powerful method, and is far simpler than the C++ method
of having a couple pages of match levels which nobody understands. Tellingly,
C++ switch from match levels to partial ordering for template overload
resolution.

D benefited by this experience and uses partial ordering for both functions and
templates.

The compiler is correctly implementing the language semantics.

--


More information about the Digitalmars-d-bugs mailing list