[nem-en] Pattern matching outside of 'match' block?
Michal Moskal
michal.moskal at gmail.com
Wed Feb 21 18:39:38 CET 2007
On 2/21/07, Vladimir Reshetnikov <v.reshetnikov at gmail.com> wrote:
> Is it possible to perform pattern matching outside of 'match' block?
> I mean a simple test, returning boolean value, to determine does the
> given value match the specified pattern or not.
> I looked for a suitable production among primary and secondary
> expressions in language reference
> (http://nemerle.org/Reference_Manual), but without success :(
> So, I tried the following.
>
> [nemerle]
> Foo(o : object) : list[bool] {
> mutable s = [];
> s ::= o is int; // (1)
> s ::= o is (int * int); // (2)
> s ::= o is (int -> int); // (3)
> s;
> }
> [/nemerle]
>
> Line marked (1) works well, but lines (2) and (3) do not.
> They do not compile at all.
The reason is that 'is' is used in two scenarios:
1. type tests, so you can do expr is int, expr is list[int] and the like
2. matching tests, so you can do expr is [_]
The problem is that depending on the part after 'is' we select either
type test of matching test, and it seems we do not treat tuple or
function types as types there, but as patterns (which are invalid). So
you can either use: expr is (_ is int * int) (which is ugly), or fix
CanBeTypeName in Typer.n ;-)
--
Michał
More information about the devel-en
mailing list