pitfalls of enum

JR zorael at gmail.com
Sat Nov 2 14:14:37 PDT 2013


(TL;DR: when to avoid enum?)

 From the dlang.org page on enums;
> Enum declarations are used to define a group of constants. They 
> come in these forms:
> 
> Named enums, which have an EnumTag.
> Anonymous enums, which do not have an EnumTag.
> Manifest constants.

Quoth Dmitry Olshansky in my thread on ctRegex and GC pressure[1]:
> The problem is that ctRegex returns you a pack of 
> datastructures (=arrays).
> Using them with enum makes it behave as if you pasted them as 
> array literals and these do allocate each time.
> 
> TL;DR: use static and/or auto with ctRegex not enum.

1. I see the use and convenience of multi-member enums, be they 
anonymous or named.
2. I understand it's the preferred way of forcing something to be 
evaluated at compile-time. The language page does not mention 
this, but I believe TDPL did?
3. I understand that enums are used to define the value of 
eponymous template instantiations. (But my snowflake self finds 
it an unintuitive and ad-hoc way of declaration.)
4. I gather that you should think of enum contents as being 
copy/pasted to wherever they are used.
5. I understand there's a hidden gotcha with (4) and types that 
allocate.
6. I understand that, given a string, instantiating any number of 
other string variables with the value of the first will simply 
alias them to the same immutable char array. (string abc = "def"; 
string ghi = abc;)

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? What if it were a class? A 
string? A string instantiated with the value of another string, 
which would normally simply create an alias?

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


[1]: http://forum.dlang.org/post/l2456h$18jk$1@digitalmars.com
[2]: http://forum.dlang.org/post/l4gnrc$2glg$1@digitalmars.com


TFMIU. ;>


More information about the Digitalmars-d-learn mailing list