Inferred enum base types

Jonathan M Davis jmdavisProg at gmx.com
Fri Oct 14 12:00:33 PDT 2011


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.

I would point out however, that you wouldn't want it to be char anyway. You'd 
want it to be dchar. Since char uses a multi-byte encoding, you're just 
begging for bugs if you operate on individual chars (which is why I think that 
'a' should be inferred to be dchar, but that's a separate issue).

- Jonathan M Davis


More information about the Digitalmars-d mailing list