rsdn / nemerle

Nemerle language. Main repository.
http://nemerle.org
Other
616 stars 89 forks source link

Types pattern matching #553

Open NN--- opened 10 years ago

NN--- commented 10 years ago

So you can write this:

class A {}

def a = typeof(A);
match (a)
{
| typeof(A) => ...
| typeof(B) =>
}

This code is literally similar to this:

match (a)
{
| x when x.Equals(typeof(A)) => ...
| x when x.Equals(typeof(B)) =>
}
Stitchous commented 10 years ago

with one small modification of the active pattern macro you can write something like this:

def s = typeof(int);

 active match(s)
 {
   | typeof(string) => WriteLine("string")
   | typeof(int) => WriteLine("int")
}

:)

NN--- commented 10 years ago

Interesting, I didn't think about active pattern. There is ExtensionPattern macro which can be used together with regular match.