UnguardedInsert
rascal-0.40.17
Synopsis
An insert
occurs outside a visit
expression.
Description
An insert statement may only occur in the action part of a pattern with action, more precisely in a case in a visit expression.
Remedies:
- Use an auxiliary variable and list or set operations to insert the value where you want.
- Place the insert statement inside a visit.
Examples
Here is an example of the use of insert to swap the arguments of red nodes:
Our favorite data type, colored trees:
rascal>data CTree = leaf(int n) | red(CTree left, CTree right) | green(CTree left, CTree right);
ok
An example tree:
rascal>CTree T = red(green(leaf(1), red(leaf(2), leaf(3))), red(leaf(4), leaf(5)));
CTree: red(
green(
leaf(1),
red(
leaf(2),
leaf(3))),
red(
leaf(4),
leaf(5)))
A visit to swap the arguments of red nodes:
rascal>visit(T){ case red(CTree l, CTree r): insert red(r,l); }
CTree: red(
red(
leaf(5),
leaf(4)),
green(
leaf(1),
red(
leaf(3),
leaf(2))))
An error occurs when insert is used outside a visit:
rascal>insert red(leaf(1), leaf(2));
|prompt:///|(0,29,<1,0>,<1,29>): Insert statement outside a visit statement
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UnguardedInsert|
ok