Bug 3999 and 4261

bearophile bearophileHUGS at lycos.com
Tue Aug 31 18:46:23 PDT 2010


Daniel Gibson:

> Why not use the non-fictional const keyword? "The const attribute 
> declares constants that can be evaluated at compile time."[1]

But you can use const for constants that are known at run-time only. While you can't use enum for constant known at run-time.


> "enum int n = 10;" looks really strange. I don't know if this is needed..

I think it helps in a linker issue. But I am not expert on this. Maybe LDC (based on LLVM) doesn't need enums much.


> But "enum : int { FOO, BAR, BAZ };" does not look so strange to me and 
> I'd prefer this to "const int FOO=0; const int BAR=1; const int BAZ=2;".
> The syntax is shorter, it shows that these keywords kind of belong 
> together and the values are enumerated automatically.

I agree, and I have never said I want to disallow it. You can cast those FOO, BAR, etc implicitly to int. It's the example V2 I have shown, no error there:

enum V1 = 10;
enum { V2 = 20 }
enum Foo { V3 }
void main() {
    assert(V1 == 10);    // OK
    assert(V2 == 20);    // OK
    assert(Foo.V3 == 0); // ERROR, type mismatch
}

Thank you for your doubts and questions, they help me express better what I meant. It seems I was not clear enough at the beginning.

Bye,
bearophile


More information about the Digitalmars-d mailing list