[Issue 596] Support array, arrayliteral and struct in switch and case

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jan 30 06:39:20 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=596



--- Comment #4 from bearophile_hugs at 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: -------


More information about the Digitalmars-d-bugs mailing list