[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:16:05 PST 2013


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


Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras at gmail.com


--- Comment #11 from Simen Kjaeraas <simen.kjaras at gmail.com> 2013-03-03 10:16:01 PST ---
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:

struct DontCare {
    bool opEquals( T )( T other ) const {
        return true;
    }
}

enum DontCare dontCare = DontCare( );

unittest {
    import std.typecons : Tuple, tuple;

    static class A {}

    auto a = tuple( 12, "foo" );
    assert( a == tuple( 12, dontCare ) );
    assert( a == tuple( dontCare, "foo" ) );
    assert( a != tuple( 42, dontCare ) );
    assert( a != tuple( dontCare, "bar" ) );
    assert( tuple( 12, dontCare ) == a );
    assert( tuple( dontCare, "foo" ) == a );
    assert( tuple( 42, dontCare ) != a );
    assert( tuple( dontCare, "bar" ) != a );
    auto aa = new A( );
    auto b = tuple( 1.5f, aa );
    assert( tuple( 1.5f, dontCare ) == b );
    assert( tuple( dontCare, aa ) == b );
    assert( tuple( 3.5f, dontCare ) != b );
    assert( tuple( dontCare, cast(A)null ) != b );
    assert( b == tuple( 1.5f, dontCare ) );
    assert( b == tuple( dontCare, aa ) );
    assert( b != tuple( 3.5f, dontCare ) );
    assert( b != tuple( dontCare, cast(A)null ) );
}


This would allow switch statements like this:

switch (myTuple) {
    case tuple( 3, dontCare ): // A
        break;
    case tuple( dontCare, "foo" ): // B
        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