www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - switch using a variable that can cast to both integer and string

reply "Tommi" <tommitissari hotmail.com> writes:
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 understand multiple alias this statements will be possible at 
some point?
Sep 26 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 26 2012
parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
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.
Sep 26 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 26 2012
parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Wednesday, 26 September 2012 at 08:37:33 UTC, Jonathan M Davis 
wrote:
 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.
Agreed. This should get the same treatment: void foo(int); void foo(string); foo(MyStruct.init); // should be ambiguous
Sep 26 2012