Is enum static?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Aug 8 15:40:25 PDT 2013


On Thu, Aug 08, 2013 at 06:38:18PM -0400, Jonathan M Davis wrote:
> On Thursday, August 08, 2013 14:49:29 Ali Çehreli wrote:
> > On 08/08/2013 02:45 PM, Borislav Kosharov wrote:
> > > If I have any enum in a class is it one for all instances or one
> > > per instance? Also are enums one per thread or only one?
> > 
> > More than that. :) enums are manifest constants.
> > 
> > Imagine that enum as being pasted into source code as is. This used
> > to have surprising effects for AAs, as an enum AA would be
> > instantiated from scratch everywhere that AA enum was used in the
> > code. Perhaps it is still that way...
> 
> I'm sure that it's still that way. When you use an enum, you're
> effectively copy-pasting its definition, so you end up with a separate
> copy of it every time you use it. That's not a big deal for value
> types or string literals, but for arrays or AAs, it can result in a
> lot of allocations that you may not have wanted.
[...]

Are we going to fix this anytime soon (or at all)? If not, we'd better
start documenting this, since currently enums are being sold as manifest
constants, and most people would understand that as meaning it's only
allocated once at compile-time/startup-time. It would leave a very bad
impression if new users unknowingly end up with a lot of unwanted
allocations just from using a language feature as-advertised.


T

-- 
There's light at the end of the tunnel. It's the oncoming train.


More information about the Digitalmars-d-learn mailing list