www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - immutable string

reply "Michael" <pr m1xa.com> writes:
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?
Apr 27 2013
next sibling parent "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Saturday, 27 April 2013 at 18:14:11 UTC, Michael wrote:
 Why?
Probably outdated documentation (and in upcoming 2.063 this will be enforced properly). Use enum instead.
Apr 27 2013
prev sibling next sibling parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
On Saturday, 27 April 2013 at 18:14:11 UTC, 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 maybe string is already (immutable)char[]?
Apr 27 2013
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 27 April 2013 at 21:46:03 UTC, Minas Mina wrote:
 On Saturday, 27 April 2013 at 18:14:11 UTC, 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 maybe string is already (immutable)char[]?
immutable string is converted to immutable(string) which is converted to immutable(immutable(char)[]). So no error with that.
Apr 27 2013
prev sibling parent "Michael" <pr m1xa.com> writes:
 Why?
Because maybe string is already (immutable)char[]?
Immutable var itself is runtime constant (as mentioned here - docs are outdated), enum var is manifest constant (can be copypasted at compile time). Now right way is 'enum string' ... or 'str.to!string()' that can be evaluated at compile time. P.S.: Found this in previous similar topic.
Apr 28 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Apr 27 2013