Sometimes it is handy to use a same variable name in different cells. For example, I want to test several differenty ways to interpolation a function. I will define a interpolation instance in one cell, say
spl = Interpolation1()
and later I want to test another interpolation
spl = Interpolation2()
This is not working because Pluto.jl does not let me use spl again. Instead, I have to write
spl2 = Interpolation2()
But this is sometimes very annoying to do so. In the normal REPL or Jupyter notebooks, I can simple resue the
but I understand the reason why authors did not permit it.
Yes, it is annoying but how would you combine reactivity and variable reuse? I think reactivity is so useful that I can live with slight annoyance …
Yeah, I know that. But is it possible to have an option that allow us to use it as an normal notebook? I love the interface of Pluto.jl very much (especially the doc panel!). Sometimes reactivity is not that essential and I can live without it. I know in this situation I can use Jupyter instead. But the dream is I can use Pluto.jl all the time
AFAIK this is part of the design — Pluto.jl builds up a graph of how variables depend on each other. You can even put them out of order and it will do the right thing. Magic!
Reusing variables would make this impossible. But you can try to avoid repetitive code instead and just write something like
test_interpolation(x, I) = plot(x, I(x))
I’ve heard an opinion from a person teaching programming that allowing re-definition is a downside of Jupyter as a teaching environment.
Like, a student may delete a cell but have the evaluated stuff still affecting the behavior within the session. Or redefine a function or variable, move to another part of the notebook, forget about that re-definition in the process and have questions why is nothing working. And it’s hard for the teacher to address those questions, as the evaluation order is different from the cell order, part of the state may be already deleted etc.
So, the choice made or Pluto.jl is a blessing for at least one quite important (IMO) scenario. For that reason, I am happy to use workarounds even if it is slightly annoying.
Your concrete example will work if you replace
let, thus creating a local scope for
But this is only a solution when the variable is only needed inside the cell, not outside.
I have marked this approach for the solution. However, I also found @Tamas_Papp 's solution very useful. I will use these approaches to suit my need. Thanks all for the great inputs!
I find it a bit annoying when using VegaLite in a Pluto.jl notebook. I can get around it by putting the variable names in quotes.