AndreVanDelft / scala

The SubScript extension to the Scala programming language
http://www.subscript-lang.org/
12 stars 1 forks source link

Output parameters and constrained parameters for script lambda's #13

Open AndreVanDelft opened 10 years ago

AndreVanDelft commented 10 years ago

Output parameters (?p) and constrained parameters (??p) are not yet supported for script lambda's. A lambda expression such as

(?i:Int) => [expr]

should be parsed well and transformed into

(_i:FormalOutputParameter[Int]) => [expr (with i replaced by _i.value)]

etc., like scriptLocalDataTransformer does in https://github.com/AndreVanDelft/scala/blob/develop/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala (around line 1700).

A type

    ?Int => Script

should be parsed and rewritten into

    FormalOutputParameter[Int] => Script

The hard part will be the parsing: accept the optional ? and ?? at the start of a parameter list, just after the '(' (or after "case" in a partial function), or maybe even harder: just before a type name.

It may be worthwile to support all these transformations as well for normal methods and for class parameters, rather than for scripts only. The Scala code in https://github.com/AndreVanDelft/scala/blob/develop/src/subscript/subscript/swing/Scripts.scala contains currently phrases such as

case class KeyTypedReactor[...](..., keyCode: FormalConstrainedParameter[Char]) ... {
  ...
    if (keyCode.matches(char)) {
      keyCode.value = char
      ...
    }
    ...
}

Instead we should be able to write:

case class KeyTypedReactor[...](..., ??keyCode:Char) ... {
  ...
    if (_keyCode.matches(char)) {
      keyCode = char
      ...
    }
    ...
}