Mutable enums

so so at so.so
Mon Nov 14 16:44:12 PST 2011


On Mon, 14 Nov 2011 15:13:20 +0200, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> There is definitely some debatable practice here for wherever enum is  
> used on an array.
>
> Consider that:
>
> enum a = "hello";
>
> foo(a);
>
> Does not allocate heap memory, even though "hello" is a reference type.
>
> However:
>
> enum a = ['h', 'e', 'l', 'l', 'o'];
>
> foo(a);
>
> Allocates heap memory every time a is *used*.  This is  
> counter-intuitive, one uses enum to define things using the compiler,  
> not during runtime.  It's used to invoke CTFE, to avoid heap  
> allocation.  It's not a glorified #define macro.

Thanks Steve, this is exactly what i was trying to say.

> The deep issue here is not that enum is used as a manifest constant, but  
> rather the fact that enum can map to a *function call* rather than the  
> *result* of that function call.
>
> Would you say this should be acceptable?
>
> enum a = malloc(5);
>
> foo(a); // calls malloc(5) and passes the result to foo.
>
> If the [...] form is an acceptable enum, I contend that malloc should be  
> acceptable as well.
>
> My view is that enum should only be acceptable on data that is  
> immutable, or implicitly cast to immutable, and should *never* map to an  
> expression that calls a function during runtime.

I agree, enum in its current shape implies just that.

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list