Enums, typedefs and lowest common denominators
Stewart Gordon
smjg_1998 at yahoo.com
Mon Nov 27 09:37:36 PST 2006
http://www.digitalmars.com/d/type.html
"# If one operand is a typedef and the other is the base type of that
typedef, the result is the base type.
# If the two operands are different typedefs but of the same base type,
then the result is that base type."
Something's puzzling here. I first noticed a problem when the SDWF
example program MDI Edit stopped compiling. For the record, the error is:
----------
mdiedit.d(284): function smjg.libs.sdwf.windowbase.WindowBase.style ()
does not match parameter types (uint)
mdiedit.d(284): Error: cannot implicitly convert expression
(((cast(WindowBase)(this.editBox)).style)() | cast(ES)cast(WS)256u) of
type uint to WS
----------
This appears to be due to promotion rules that have been in place for a
while, but the problem with this code has only just manifested in the
light of the fix to bug 349.
http://d.puremagic.com/issues/show_bug.cgi?id=349
Anyway, back to the point. It would appear that by "base type" the spec
means the built-in (or class or struct or union) type from which the
typedef is ultimately derived.
This spec doesn't mention anything about enums, but the compiler seems
to be treating enums and typedefs as a single entity in this respect.
For example:
----------
import std.stdio;
typedef uint EnumBase;
enum Enum1 : EnumBase { Value1, Value2 }
enum Enum2 : EnumBase { Value3, Value4 }
void test(EnumBase e) {
writefln("EnumBase: %d", e);
}
void test(uint e) {
writefln("uint: %d", e);
}
void main() {
test(Enum1.Value1 | Enum2.Value4);
}
----------
shows that the enum values are promoted to uint, which is the ultimate
base type of both enums, as passing through both enums and typedefs.
When I suggested OUAT how typedef arithmetic should work, I intended
typedefs with a common base type to promote to the lowest common
denominator, i.e. the most-derived typedef that is a common base to
both. It would make sense to apply this logic also to enums, and even
to chains of typedefs and enums.
But the spec certainly has holes here. Firstly, it ought to clarify
what is meant by "base type"; secondly, it needs to mention what happens
to enums in the same circumstance.
Under the lowest common denominator principle, my code would compile
without trouble. But for as long as they are always promoted to the
_ultimate_ base type if at all, it's hard to make these hierarchical
enums work properly and keep SDWF user-friendly to this degree. It's
nice if people can still use
editBox.style = editBox.style | ES.NOHIDESEL;
rather than
editBox.style = cast(WS) (editBox.style | ES.NOHIDESEL);
Comments?
Stewart.
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS-
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
More information about the Digitalmars-d
mailing list