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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 3 10:50:35 PST 2013


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



--- Comment #12 from bearophile_hugs at eml.cc 2013-03-03 10:50:27 PST ---
(In reply to comment #11)
> The problem of your proposed pattern matching is that there is not necessarily
> a simple correlation between constructor parameters and runtime values. For
> instance, for this struct declaration:
> 
> struct Foo {
>     int n;
>     this( int a, int b ) {
>         n = a * b;
>     }
> }
> 
> , Foo( 12, void ) just doesn't make any sense. For tuples however, this works:

Arrays don't have a constructor, so they don't have this problem in a switch.

Regarding structs, the problem has some solutions (both are needed):
1) Don't accept structs that have one or more constructors, and special-case
std.typecons.Tuple in the compiler so tuples are accepted (the compiler assumes
their constructor is just a series of field assignments).
2) If you want to support the general case of structs that have a constructor,
then such structs must have a standard method like "unapply", that is used by
the switch itself. This is the solution used by Scala language, it's shown in
the Comment 9: http://d.puremagic.com/issues/show_bug.cgi?id=596#c9

-- 
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