Inferred enum base types

Lennart Blanco cokebuttle at gmail.com
Sat Oct 15 06:52:39 PDT 2011


On Fri, Oct 14, 2011 at 9:00 PM, Jonathan M Davis <jmdavisProg at gmx.com>wrote:

> On Friday, October 14, 2011 20:47:12 Lennart Blanco wrote:
> > Hi
> >
> > When compiling following code with dmd (v2.051)
> >
> > enum nums {  X = 'a', Y, Z }
> >
> > void main() {
> >   nums q;
> >   char w;
> >   w = q;
> > }
> >
> > I get 'Error: cannot implicitly convert expression (q) of type nums to
> char'
> > for the 'w = q' assigment.
> >
> > Should'n the inferred base type for 'nums' be char, given that the first
> > 'nums' member is initilized with char literal 'a'?
> >
> > When the nums declaration is changed to:
> >
> > enum nums : char {  X = 'a', Y, Z }
> >
> > the code compiles without errors.
> >
> > It seems that when enums base type is allways inferred to int. Am I
> missing
> > something or is it an DMD bug?
>
> I believe that the answer is that there is effectively no inferrence when
> you
> give a list of enum values (rather than declaring a manifest constant), and
> it
> is always assumed to be int unless you declare it to be otherwise.
>
>
The reason I expected the base type to be inferred to char is because the
language spec (http://d-programming-language.org/enum.html) says:

"If the EnumBaseType is not explicitly set, and the first EnumMember has an
initializer, it is set to the type of that initializer. Otherwise, it
defaults to type int."

I have now checked TDPL book and there it only says 'The type of named enum
is automatically deduced as int'. No mention of using first enum member
initializer for deducing the base type.

Perhaps the problem is that language spec need to be updated?

/lennart
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111015/aa38ce80/attachment.html>


More information about the Digitalmars-d mailing list