Is enum static?

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 20 14:01:30 PDT 2013


On Tuesday, August 20, 2013 22:10:28 JS wrote:
> On Monday, 19 August 2013 at 18:28:10 UTC, Ali Çehreli wrote:
> > On 08/19/2013 03:18 AM, Borislav Kosharov wrote:> So if I want
> > 
> > to have a string constant it is a lot better to declare it as:
> > > static immutable string MY_STRING = "Some string";
> > > 
> > > Because it won't be duplicated?
> > 
> > enum is fine with strings.
> > 
> > It is a common space optimization of the compilers not to
> > duplicate identical strings. The following program includes
> 
> > just one "hello world" in the compiled object file:
> And why can't this be done with any compile time objects? AA's
> that are identical be collated similar to strings.

It only works with string literal because they're immutable, and the compiler 
optimizes for that by only creating them once (it even puts them in the read-
only portion of the binary in Linux). The same doesn't hold true for stuff like 
AAs.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list