www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 596] New: Support array, arrayliteral and struct in switch and case

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596

           Summary: Support array, arrayliteral and struct in switch and
                    case
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: lovesyao hotmail.com


struct Test{
  int x;
  int y;
  int z;
}

void main(){
  Test* t = new Test;
  const static Test c1 = {0,0,0};
  switch(*t){
    case c1://struct
      break;
    default:
      assert(0);
  }
  ubyte[] t2 = [0,1,2];
  switch(t2){
    case [0,1,2]://arrayliteral
      break;
    default:
  }
}


-- 
Nov 25 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-11-25:
 http://d.puremagic.com/issues/show_bug.cgi?id=596

 void main(){

<snip>
   ubyte[] t2 = [0,1,2];
   switch(t2){
     case [0,1,2]://arrayliteral
       break;
     default:
   }
 }

Added to DStress as http://dstress.kuehne.cn/run/c/case_05_A.d http://dstress.kuehne.cn/run/c/case_05_B.d http://dstress.kuehne.cn/run/c/case_05_C.d http://dstress.kuehne.cn/run/s/switch_23_A.d http://dstress.kuehne.cn/run/s/switch_23_B.d http://dstress.kuehne.cn/run/s/switch_23_C.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFaIn9LK5blCcjpWoRAmSzAKCoAbyM7GpWeT91ns333aGZOpS/pQCffTPD kFNVqQQA11CBcTe8BRZfUS4= =XNA6 -----END PGP SIGNATURE-----
Nov 25 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #2 from bearophile_hugs eml.cc 2010-11-26 12:37:59 PST ---
If structs become supported by switch, then an interesting use case is to
support Tuples of typecons too.

(A possible enhancement is to support class references too (comparing the
dynamic type), but similar switches on objects is considered a bad practice in
OO code. So maybe it's better to not support class references).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596



--- Comment #3 from bearophile_hugs eml.cc 2011-01-07 05:13:09 PST ---
A possible improvement for the switch on structs is to support wildcards in
some way:

struct Foo { int x, y; }
void main() {
    auto f = Foo(1, 2);
    int f;
    switch (f) {
        case Foo(1, _): break; // all structs where x==1, y == don't care
        default: break;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596



--- Comment #4 from bearophile_hugs eml.cc 2011-01-30 06:37:06 PST ---
(In reply to comment #3)

 struct Foo { int x, y; }
 void main() {
     auto f = Foo(1, 2);
     int f;
     switch (f) {
         case Foo(1, _): break; // all structs where x==1, y == don't care
         default: break;
     }
 }

The idea of wildcards is very useful, it turns the D switch into something useful to partially match Tuples too. This is a very commonly used operation in functional languages. But probably the "_" (underscore) is not right as wildcard, because while it's nice and clear, it's a legal variable name. Possible alternatives are a single &, or or %: case Foo(1, &): break; case Foo(1, ): break; case Foo(1, %): break; Or maybe just "void", that is longer, but adds no new tokens and is more clear than an arbitrary symbol: import std.typecons: Tuple; alias Tuple!(int, "x", int, "y") Foo; void main() { auto f = Foo(1, 2); switch (f) { case Foo(1, void): break; // any Foo with x==1, don't care y default: break; } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 30 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=596



--- Comment #5 from bearophile_hugs eml.cc 2011-03-21 18:24:53 PDT ---
If structs too gets supported by switch, then BigInts too become allowed:


import std.bigint;
void main() {
    auto x = BigInt(3);
    switch (x) {
        case BigInt(0): break;
        default: break;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 21 2011