[nem-en] literal pattern matching

Wiktor Zychla wzychla at vulcan.edu.pl
Fri Sep 24 10:07:14 CEST 2004


according to the documentation it is possible to match constant literals in 
case of value types:

StringOfInt (n : int) : string
{
  match (n) {
    | 0 => "null"
    | 1 => "one"
    | 2 => "two"
    | 3 => "three"
    | _ => "more"
  }
}

would it ever be possible to match "by value" with bound variables? I think 
that this could be ellegant and what's important - superior to C# where you 
cannot use variables in case branches.

following code compiles with warnings but does not work as one could expect 
since the n is always matched with k.
Note, that k is bound inside the method body and should probably not be 
treated like a free-variable to match with any value of n. Instead, as bound 
variable, it probably should be matched "by value".

What is your opinion on this?

Wiktor

class Hello
{
 static Foo (n : int, k : int) : string {
   match (n) {
     | k => "equal to k";
     | 0 => "null"
     | 1 => "one"
     | 2 => "two"
     | 3 => "three"
     | _ => "more"
   }
 }

 static Main() : void
 {
  Console.WriteLine( Foo( 5, 3 ) );
 }
}

>out.exe
equal to k 






More information about the devel-en mailing list