Should all enums be immutable?
Stewart Gordon
smjg_1998 at yahoo.com
Tue Apr 5 16:18:37 PDT 2011
On 04/04/2011 10:32, Don wrote:
<snip>
> Yes. The ONLY reason those manifest constants exist at all, is so that they don't exist at
> run time. They have no address. Any case where taking a reference to them works, is a
> compiler bug.
<snip>
Eh what? In the case of simple value-type constants, this makes sense. But when they're
reference types, many copies exist at run time, rather than only one. This is certainly
not an advantage on any level.
A reasonable way of doing it is such that every instance of the same enum value has the
same bit pattern. For pure value types (primitive types, static arrays and structs/unions
consisting only of pure value types) this is straightforward. If a reference type is
involved, put the contents in the static data segment and have the enum value referencing
this one instance. I presume this can be done...?
Stewart.
More information about the Digitalmars-d
mailing list