[nem-en] RFC: matching variant options against non-variant values

Michal Moskal michal.moskal at gmail.com
Mon Apr 30 17:26:45 CEST 2007


Hi,

I've just commited a change on trunk to disallow the following code:

foo (o : object) : void
{
  match (o) {
    | [ x, y ] => ... x + y ...
    | _ => ...
  }
}

It seems there were several cases in the compiler source code when a
TyVar was matched against a pattern of type MType.Something. The
correct way to do it, is to match the result of Fix(), otherwise we
might get false negatives in matching, for example for aliased
variables. I guess this didn't happen, because this was mostly used on
top-level types, which are all-MType most of the time, but they were
bugs waiting to happen. I know I have written most of the code in
question myself, so it just highlights, that allowing this kind of
matching leads to problems.

There are also two bug reports releated to this, were we try to cast o
to list[object], which doesn't work.

I'm for disallowing it altogether, but if others feel like it, we I
can replace it with a (optional?) warning. The correct code is:

foo (o : object) : void
{
  match (o) {
    | o is list [int] =>
      match (o)
         | [ x, y ] => ... x + y ...
         | _ => ...
      }
    | _ => ...
  }
}

Cheers!
   Michał


More information about the devel-en mailing list