www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - switch case expressions

reply "Martin Krejcirik" <mk-junk i-line.cz> writes:
void main(string[] args)
{
     int a = 1;
     int b = to!int(args[1]);
     uint c = 2;

     switch (a)
     {
         case b: break; // OK
         case c: break; // Error: variable c cannot be read at 
compile time

         default: break;
     }
}

Switch spec says:
The case expressions must all evaluate to a constant value or 
array, or a runtime initialized const or immutable variable of 
integral type. They must be implicitly convertible to the type of 
the switch Expression.

So, should the case b compile or not ? Is the spec too 
restrictive here, or is it a bug ?

I was initially working to fix the case c (to allow casts), but 
this should be clarified.
Apr 23 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Martin Krejcirik:

 So, should the case b compile or not ? Is the spec too 
 restrictive here, or is it a bug ?
Apparently it's a WONTFIX mess. The spec should be updated. Walter&Andrei refused to fix a design bug here. Bye, bearophile
Apr 23 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/23/15 4:25 PM, bearophile wrote:
 Martin Krejcirik:

 So, should the case b compile or not ? Is the spec too restrictive
 here, or is it a bug ?
Apparently it's a WONTFIX mess. The spec should be updated. Walter&Andrei refused to fix a design bug here.
Source? IMO, the OP code does not warrant the errors cited. -Steve
Apr 23 2015
parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thursday, April 23, 2015 16:29:03 Steven Schveighoffer via Digitalmars-d
wrote:
 On 4/23/15 4:25 PM, bearophile wrote:
 Martin Krejcirik:

 So, should the case b compile or not ? Is the spec too restrictive
 here, or is it a bug ?
Apparently it's a WONTFIX mess. The spec should be updated. Walter&Andrei refused to fix a design bug here.
Source? IMO, the OP code does not warrant the errors cited.
Well, there's this mess https://issues.dlang.org/show_bug.cgi?id=6176 but I don't see anything in there from Walter or Andrei, so I'm not sure what Bearophile is referring to. Personally though, I wish that case statements only allowed compile-time constants... :| - Jonathan M Davis
Apr 23 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/23/15 5:04 PM, Jonathan M Davis via Digitalmars-d wrote:
 On Thursday, April 23, 2015 16:29:03 Steven Schveighoffer via Digitalmars-d
wrote:
 On 4/23/15 4:25 PM, bearophile wrote:
 Martin Krejcirik:

 So, should the case b compile or not ? Is the spec too restrictive
 here, or is it a bug ?
Apparently it's a WONTFIX mess. The spec should be updated. Walter&Andrei refused to fix a design bug here.
Source? IMO, the OP code does not warrant the errors cited.
Well, there's this mess https://issues.dlang.org/show_bug.cgi?id=6176 but I don't see anything in there from Walter or Andrei, so I'm not sure what Bearophile is referring to. Personally though, I wish that case statements only allowed compile-time constants... :| - Jonathan M Davis
Found it by following some references: https://github.com/D-Programming-Language/dmd/pull/2887 Ugh... at the VERY LEAST, the error message should be fixed, as "can't be read at compile time" is clearly not the issue. -Steve
Apr 23 2015
parent "Martin Krejcirik" <mk-junk i-line.cz> writes:
 https://github.com/D-Programming-Language/dmd/pull/2887

 Ugh...

 at the VERY LEAST, the error message should be fixed, as "can't 
 be read at compile time" is clearly not the issue.
Looks like this issue surfaces periodically. Somehow I've missed it so far. Anyway, either the spec is wrong or the implementation is wrong.
 at the VERY LEAST, the error message should be fixed, as "can't 
 be read at compile time" is clearly not the issue.
This is auto solved by allowing cast in case expression.
Apr 23 2015