Opportunities for D
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jul 9 00:10:08 PDT 2014
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.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list