rascal
rascal copied to clipboard
enhancement: List matching on argument lists
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.
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
nodein 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
makedoes from theTypemodule. - like
getChildrenis hidden by matching,makewill be hidden by splicing.