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

Jonathan M Davis jmdavisProg at gmx.com
Wed Sep 26 01:02:49 PDT 2012


On Wednesday, September 26, 2012 09:54:02 Tommi wrote:
> This bug report:
> http://d.puremagic.com/issues/show_bug.cgi?id=7979
> ... made me think about the following scenario:
> 
> struct MyStruct
> {
>      int    _intValue;
>      string _strValue;
> 
>      alias _intValue this;
>      alias _strValue this;
> }
> 
> void main()
> {
>      auto ms = MyStruct(1, "two");
> 
>      switch (ms)
>      {
>      case MyStruct(1, "one"):
>          // To be here...
>          break;
> 
>      case MyStruct(2, "two"):
>          // ... or here?
>          break;
> 
>      default:
>          // Not here though
>      }
> }
> 
> Should that switch statement:
> a) Give an error saying "ms is neither integral nor string"
> b) Cast its conditional- and case-expressions to int
> c) Cast its conditional- and case-expressions to string

I would expect it to give errors on those case statements, because they're not 
actually ints or strings. I'd also expect an error if not all of the case 
statements had the same type. But I don't know what the compiler actually 
does. Certainly, the example won't compile as-is regardless, because multiple 
alias thises isn't supported yet.

> I understand multiple alias this statements will be possible at
> some point?

Yes. TDPL says that D's supposed to allow them. It just hasn't been 
implemented in the compiler yet.

- Jonathan M Davis


More information about the Digitalmars-d mailing list