enum VS static immutable
w0rp
devw0rp at gmail.com
Thu Mar 13 07:46:12 PDT 2014
On Thursday, 13 March 2014 at 14:38:27 UTC, ref2401 wrote:
> Hi.
> I have this structure:
>
> struct MyStruct {
> enum MyStruct VALUE = MyStruct(5f);
>
> static immutable MyStruct value = MyStruct(5f);
>
>
> float data;
>
> this(float v) { data = v; }
> }
>
> What's the difference between MyStruct.VALUE and
> MyStruct.value? How should I decide what to use?
enum is a compile-time constant. 'static immutable' is an
immutable (and therefore implicitly shared between threads...)
runtime constant that isn't part of the struct itself. I'd stick
with enum and switch to static when what you're writing becomes
impossible to express at compile-time. A surprising number of
things can actually be expressed at compile-time.
More information about the Digitalmars-d-learn
mailing list