Type checking on typed enum

bearophile bearophileHUGS at lycos.com
Sun Sep 16 17:17:01 PDT 2012


ixid:

> 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.

It's a safe and quite feature of D. Unfortunately for 
implementation simplicity (and maybe compilation performance) 
this kind of static analysis is limited to single expressions. 
This limits its usefulness (and sometimes forces you to write 
larger single expressions to avoid casts, instead of assigning 
parts of an expression to intermediate variables).

Don and Hara have discussed a bit the possibility of extending 
this D feature (value range propagation) to include simple cases 
like this, that currently don't compile:

void main() {
     ubyte[10] array;
     foreach (i, ref x; array)
         x = i;
}


Even for a simple compiler it's not hard to see that this 'i' 
will not go past the max value of an ubyte. There are C lints 
that toady are able to do far more than this, and they run very 
fast.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list