switch using a variable that can cast to both integer and string

Jonathan M Davis jmdavisProg at gmx.com
Wed Sep 26 01:37:51 PDT 2012


On Wednesday, September 26, 2012 10:10:47 Peter Alexander wrote:
> On Wednesday, 26 September 2012 at 08:02:24 UTC, Jonathan M Davis
> 
> wrote:
> > I would expect it to give errors on those case statements,
> > because they're not
> > actually ints or strings.
> 
> http://dlang.org/statement.html#SwitchStatement
> 
> "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."
> 
> In the example, the case expression evaluate to a constant value,
> and they are implicitly convertible via alias this.

Then it sounds like this example would probably have to give an error due to 
ambiguity (once you can have multiple alias thises anyway), because the only 
reason that it can be used in the switch statement and cases is because it 
implicitly converts to int or string, and with the switch statement's 
expression and all of the case's expressions being implictly convertible to 
both int and string but not actually being int or string, it's ambiguous as to 
which to convert to.

- Jonathan M Davis


More information about the Digitalmars-d mailing list