Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 25, 2006 [Issue 596] New: Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
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: } } -- |
November 25, 2006 Re: [Issue 596] New: Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail Attachments: | 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 |
November 26, 2010 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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: ------- |
November 26, 2010 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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: ------- |
January 07, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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: ------- |
January 30, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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: ------- |
March 22, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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: ------- |
July 23, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=596 --- Comment #6 from bearophile_hugs@eml.cc 2011-07-23 12:15:36 PDT --- Regarding tuple unpacking see also issue 6365 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
August 15, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=596 --- Comment #7 from bearophile_hugs@eml.cc 2011-08-15 16:44:27 PDT --- One more useful use is with OOP (this is GUI code): import core.stdc.stdio; class Control {} class Slider : Control {} class Button : Control {} void main() { Control c = new Button; switch (typeof(c)) { case Slider: printf("A"); break; case Button: printf("B"); break; default: // probably a Control } } That is similar to this (but faster): import core.stdc.stdio; class Control {} class Slider : Control {} class Button : Control {} void main() { Control c = new Button; if (cast(Slider)c) { printf("A"); } else if (cast(Button)c) { printf("B"); } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
September 29, 2011 [Issue 596] Support array, arrayliteral and struct in switch and case | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=596 --- Comment #8 from bearophile_hugs@eml.cc 2011-09-28 19:13:23 PDT --- Supporting something like this will be very useful (this is done very commonly in functional languages): import std.variant: Algebraic; alias Algebraic!(int, float) A; void main() { A a = 1.5; final switch (a.type()) { case typeid(int): ... case typeid(float): ... } } But if possible I suggest to introduce a compiler optimization specific for this usage, to avoid actually allocating and using TypeInfo class instances. The advantage of using a final switch instead of a sequence of if statements: - The code is more readable, the various cases are shown in a more ordered way; - The "final" of switch makes sure all types of the Algebraic are taken into account; - The compiler has ways to optimize this code better (sometimes avoiding many runtime tests). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation