Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 27, 2013 immutable string | ||||
---|---|---|---|---|
| ||||
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? |
April 27, 2013 Re: immutable string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | 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.
|
April 27, 2013 Re: immutable string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | 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[]?
|
April 27, 2013 Re: immutable string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | 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.
|
April 28, 2013 Re: immutable string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | 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
|
April 28, 2013 Re: immutable string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | >> 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.
|
Copyright © 1999-2021 by the D Language Foundation