rascal
rascal copied to clipboard
What to expect: common keyword parameter of data declaration and keyword parameter of constructor have the same name
Setting the stage
Module vis::Graphs
contains a data declaration that goes like this (heavily abbreviated):
data CytoLayout(CytoLayoutName name = dagre(), bool animate=false)
= cytolayout()
| breadthfirstLayout(CytoLayoutName name = CytoLayoutName::breadthfirst(), ... )
| gridLayout(CytoLayoutName name = CytoLayoutName::grid(), ...)
| ...;
The striking thing is that name
occurs here in two roles:
- as common keyword parameter of the data type
CytoLayout
- as keyword parameter of the constructors
breadthfirstLayout
andgridLayout
(and several others)
What does this mean?
Let's morph the above into something really simple
data D(int name = 10)
= d1(int name = 1)
| d2(int name = name);
and play with it:
Example | Result | Comment |
---|---|---|
d1().name |
1 |
ok |
d1(name=100).name |
100 |
ok |
d2().name |
10 |
ok |
d2(name=200).name |
200 |
ok |
How does this work on arbitrary D values? Define the getter int get(D d) = d.name;
and see what happens:
Example | Result | Comment |
---|---|---|
get(d1()) |
10 |
differs from above |
get(d1(name=100)) |
100 |
ok |
get(d2()) |
10 |
ok |
get(d2(name=200)) |
200 |
ok |
It is the different behavior of d1().name
and get(d1())
that worries me. It could point at a bug in the interpreter. I am left with the following questions:
- Is this dual role of
name
invis::Graphs
accidental or intended. If the latter, what is the intention? - Should the type checker detect this situation and warn about it? (At this very moment, letting this pass the Rascal compiler will generate incorrect Java code).