Thread overview | |||||
---|---|---|---|---|---|
|
September 23, 2011 why global immutable string variable cannot be used after "case"? | ||||
---|---|---|---|---|
| ||||
import std.stdio; immutable HELLO = "hello"; void main() { auto string = "hello"; switch(string) { case HELLO: writeln("hello"); break; default: writeln("unknown"); break; } } testCase.d(7): Error: case must be a string or an integral constant, not HELLO If immutable cannot be used, what else can be used to replace #define in C? Thanks a lot. |
September 23, 2011 Re: why global immutable string variable cannot be used after "case"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cheng Wei |
> If immutable cannot be used, what else can be used to replace #define in C?
>
> Thanks a lot.
immutables are runtime constants. For case you need a compile time constant, which you can define with enum.
enum string mycase = "value";
|
September 23, 2011 Re: why global immutable string variable cannot be used after "case"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Friday, September 23, 2011 01:38 Tobias Pankrath wrote:
> > If immutable cannot be used, what else can be used to replace #define in C?
> >
> > Thanks a lot.
>
> immutables are runtime constants. For case you need a compile time constant, which you can define with enum.
>
> enum string mycase = "value";
Or you could even reduce it to
enum mycase = "value";
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation