[nem-en] Improvement in where macro

vc vc at rsdn.ru
Wed Jan 31 04:23:50 CET 2007


I add some improvement in when macro.

Now you can write:
def x = Some(123);

when (x is Some(y))
  WriteLine(y);

// or

when (x is Some(y) when y > 100)
  WriteLine(y);

instead:
match (x) 
{
  | Some(y) when y > 100 => WriteLine(y);
  | _ => ()
}

syntax:
'when' '(' expr 'is' variant_ctor_pattern [ 'when' expr ] ')'
  expr_wich_use_vars_bound_in_pattern

In other cases 'when' work as later.

Implementation:

macro whenmacro (cond, body)
syntax ("when", "(", cond, ")", body) 
{
  def res1 = match (cond)
  {
    | <[ $subCond is $pattren ]> with guard = null
    | <[ $subCond is $pattren when $guard ]> =>
      def res2 = match (pattren)
      {
        | PT.PExpr.Call when guard != null => 
          <[ match ($subCond) { | $pattren when $guard => $body : void | _
=> () } ]>
        | PT.PExpr.Call => 
          <[ match ($subCond) { | $pattren => $body : void | _ => () } ]>
        | _ => <[ match ($cond) { | true => $body : void | _ => () } ]>
      }
      res2
    | _ => <[ match ($cond) { | true => $body : void | _ => () } ]>
  }
  res1
}




More information about the devel-en mailing list