rascal icon indicating copy to clipboard operation
rascal copied to clipboard

enhancement: List matching on argument lists

Open jurgenvinju opened this issue 2 years ago • 1 comments

Describe the feature

I'd like to be able to list-match on constructor argument lists:

"nodeName"(int x, *value rest) // match an argument list that starts with an int and then zero or more value
and(*Bool args)                          // match the `and` constructor and both Bool arg and bind them to a list[Bool] named `args`

Currently when we have generic code on the level of type node we often have to use getName and getChildren in order to abstract from the number of arguments. The list-matching feature is much more elegant and shorter in such cases.

I'm guessing that this is a large change neither in the compiler nor in the interpreter. All we need to do is call getChildren first and then match the result against the list pattern, but only if at least one of the pattern variables is a list variable.

jurgenvinju avatar Apr 11 '23 09:04 jurgenvinju

Similarly, and for the sake of symmetry, I'd like to be able to splice argument lists for constructors:

list[Bool] args = [b1, b2]
and(*args)
"nodeName"(x, *rest)
  • The static type system would always return node in such a case
  • The dynamic type system would check the validity of the constructed tree against the name of the constructor and the grammar associated to it and throw an exception just like make does from the Type module.
  • like getChildren is hidden by matching, make will be hidden by splicing.

jurgenvinju avatar Apr 11 '23 09:04 jurgenvinju