Is enum static?

Ali Çehreli acehreli at yahoo.com
Mon Aug 19 11:28:10 PDT 2013


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:

import std.stdio;

enum atModuleLevel = "hello world";

void foo()
{
     writefln(atModuleLevel);
}

void main()
{
     enum s = "hello world";
     writeln(s);
     writeln(s);
     writeln(atModuleLevel);
     foo();
}

Ali



More information about the Digitalmars-d-learn mailing list