Is enum static?

JS js.mdnq at gmail.com
Tue Aug 20 15:47:59 PDT 2013


On Tuesday, 20 August 2013 at 21:01:43 UTC, Jonathan M Davis 
wrote:
> 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

That wasn't the question, I didn't ask why D does what it does by 
why it can't do what it doesn't do.


More information about the Digitalmars-d-learn mailing list