On 11/02/2013 02:14 PM, JR wrote:

> But in Andrei's thread on tristates[2] he lists this code excerpt:
>> struct Tristate
>> {
>>     private static Tristate make(ubyte b)
>>     {
>>         Tristate r = void;
>>         r.value = b;
>>         return r;
>>     }
>>
>>     enum no = make(0), yes = make(1), unknown = make(4);  // <---
>
> Is it okay to use enum there? If so, is it because (whatwith being a
> struct) it's not on the heap?

I think it is more like because its value is known at compile time. If Tristate were a class, then the class variable r would not have any class object that it pointed to.

> What if it were a class?

I tried changing struct to class:

class Tristate
{
    ubyte value;

    private static Tristate make(ubyte b)
    {
        Tristate r = new Tristate();
        r.value = b;
        return r;
    }

    enum no = make(0), yes = make(1), unknown = make(4);
}

Error: variable deneme.Tristate.no : Unable to initialize enum with class or pointer to struct. Use static const variable instead.

So, class can't work.

> A string?

That would be ok. A compile-time string is known to the compiler and can appear in the program binary to be referenced from multiple places.

> A
> string instantiated with the value of another string, which would
> normally simply create an alias?

Would work.

> When is an enum *better* than a normal (static const/immutable) constant?

Good question. :)

Ali

Reply via email to