immutable string

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 27 18:11:33 PDT 2013


On Saturday, April 27, 2013 20:14:10 Michael wrote:
> According to http://dlang.org/const3.html
> 
> >The simplest immutable declarations use it as a storage class.
> >It can be used to declare manifest constants.
> 
> So, immutable string s = "..."; should be a manifest constant.
> 
> If it is a constant that it can be used in switch(...).
> 
> switch(someStr)
> {
>     case s: ...; // Error: case must be a string or an integral
> constant, not s.
> },
> 
> but string s = "..."; works good.
> 
> Why?

Because an immutable string _isn't_ a manifest constant. Only enums are 
manifest constants.

immutable s = "foo";

or

immutable string s = "foo";

specifically create an immutable variable. It has an address and doesn't result 
in "foo" being copy-pasted everywhere that s is used like it would if s were 
an enum. It's no different from

string s = "foo";

or

auto s = "foo";

except that when is is immutable, it's implicitly shared, and you can't mutate 
it.

- Jonathna m Davis


More information about the Digitalmars-d-learn mailing list