I am trying to write a show rule that takes references and appends something to the name of its target label:
show ref: it => {
let label_str = str(it.target).split(":")
if "addition" not in label_str {
label_str.push("addition")
}
ref(label(label_str.join(":")))
}
However, I can’t call the ref
function again at the end, because of show rule depth. The target
field is also not mutable. How can I replace the reference in this situation?
The problem is that it will always call ref recursively, leading to infinite recursion. To avoid this, you should move the ref call inside the if, and add an else clause which simply return it
. That way, if the label doesn’t have the “addition” suffix, it is added an a new reference is made. Otherwise (including sub-call), it doesn’t modify the reference and simply returns it:
#show ref: it => {
let label_str = str(it.target).split(":")
if "addition" not in label_str {
label_str.push("addition")
ref(label(label_str.join(":")))
} else {
it
}
}
Thanks a lot, now I know!