On Saturday, 17 August 2013 at 05:22:53 UTC, captaindet wrote:
import std.stdio;

enum Test{
        test2 = 2,
        test4 = 4
}

Enumeration.

enum foo = Test.test2;

Manifest Const

Test bar = Test.test4;

Runtime variable.

Enum is being abused. In the first case you are declaring an enumeration[1], these values are known at compile time.

Second you defined a manifest constant, this is what you would see from #define in C. The symbol, foo, is replaced by the right hand side when referenced.

Third you've declared a variable, bar, which will store your enumerated value, 4. Variables are not compile time, even if the value stored came from a compile time known value.

    int foobar = 5; // 5 is known at compile time, foobar is not.

pragma( msg, foo );             // why does it print: cast(Test)2

You are referring to a manifest constant, this is a simple textual replacement. Enumerations are typed, 2 is not a Test, so the compiler will write out a cast so the type system is happy. Similarly Test.test2 is not the value of foo, foo is a signal to the compiler to insert "cast(Test)2."

Hope that makes sense.

1. http://en.wikipedia.org/wiki/Enumeration

Reply via email to