Opportunities for D

Meta via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 9 06:45:49 PDT 2014


On Wednesday, 9 July 2014 at 07:10:09 UTC, Jacob Carlborg wrote:
> On 09/07/14 00:21, bearophile wrote:
>
>> 9. Built-in tuples usable in all the most important situations 
>> (with a
>> syntax that doesn't kill possible future improvements of the 
>> switch
>> statement to perform basic pattern matching on structs that 
>> have an
>> optional method named "unapply").
>
> I think it would be possible to implement pattern matching in 
> library code. Something like this:
>
> match!(value,
>     (int t) => ifInt(),
>     (char t) => ifChar(),
>     () => noOtherMatch()
> );
>
> Destructing a type could look something like this:
>
> match(value,
>     (type!(Foo), int a, int b) => doSomething(), // match if 
> value is of type Foo and extract it
>     (int a, int b) => doSomething() // match anything that can 
> be destructed to two ints
> );
>
> The syntax is not so pretty but I think it would be possible.

As far as I know, there's no reason we can't add pattern matching 
to switch or final switch or both. There's no ambiguity because 
right now it's not possible to switch on structs or classes. See 
Kenji's DIP32 for syntax for tuples that could be leveraged.


More information about the Digitalmars-d mailing list