Type checking on typed enum

ixid nuaccount at gmail.com
Sun Sep 16 16:03:46 PDT 2012


If I use code like:

enum ulong VAR = 10;

I'd have expected this to be type checked when used as a ulong. 
Instead it seems to be checked based on its absolute value and is 
accepted for a function like:

void fun(int n) {
     // Do stuff
}

If the value of VAR is set to more than int.max then the compiler 
(DMD2 latest non-beta) notices the type mismatch. This would seem 
like an understandable enum behaviour if VAR was just enum rather 
than enum ulong but intuitively I'd have expected the specifier 
to limit what would accept the enum. Is this how it's supposed to 
work? As a beginner with D I came across the advice that enum was 
the correct way to specify global constants but this makes it 
feel safer to use const when you want type checking and perhaps 
enum isn't the right choice.


More information about the Digitalmars-d-learn mailing list