Closed mortenpi closed 1 year ago
Nice catch.
An alternative solution could be to add a position counter to JuliaValue, which would get incremented every time the parser constructs a new JuliaValue.
I think I'm more partial to a counter-based approach to this rather than making JuliaValue
mutable, but if that implementation would be much more complex than the current one then we can just go with this IdDict
approach.
I've updated this with a counter-based approach. This actually allows us to get rid of the Dict
altogether, since we're literally storing the array indices in the counter now.
However, rather than just adding the .pos
field to JuliaValue
, I decided to create a new JuliaExpression
inline element that gets added to the tree during parsing, and then replaced by JuliaValue
. This has two benefits in my mind:
cm""
macro). Because of this, I also removed the one-argument constructor for JuliaValue
, implying that a JuliaValue
must always be an evaluated value.Added the writer methods.
Thanks Morten!
A variable that is interpolated multiple times with updates in between will print with the final value in all positions:
This is because
JuliaValue
is a struct and therefore the two instance ofJuliaValue(:x, nothing)
are identical. This confuses the replacementDict
in_interp!
, where value from the earlier position gets overwritten.Changing
JuliaValue
to be mutable and usingIdDict
in_interp!
fixes this:A few notes:
Strictly,
IdDict
is not necessary. However, using it makes sure it keeps working if==
andhash
were to become overloaded (which is what I do in MarkdownAST right now).An alternative solution could be to add a position counter to
JuliaValue
, which would get incremented every time the parser constructs a newJuliaValue
.This only affects pure variable interpolation, since
Expr
objects are mutable, and so won't match even if they have identical content.