Does anyone find it is a bit annoying that Pluto.jl does not let you reuse variable in different cells?

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

begin
  spl = Interpolation1()
  plot(x, spl(x))
end

and later I want to test another interpolation

begin
  spl = Interpolation2()
  plot(x, spl(x))
end

This is not working because Pluto.jl does not let me use spl again. Instead, I have to write

begin
  spl2 = Interpolation2()
  plot(x, spl2(x))
end

But this is sometimes very annoying to do so. In the normal REPL or Jupyter notebooks, I can simple resue the spl.

3 Likes

Yes,

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 :slightly_smiling_face:

2 Likes

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 :slight_smile:

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))

test_interpolation(x, Interpolation())

test_interpolation(x, Interpolation2())
8 Likes

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.

7 Likes

Your concrete example will work if you replace begin with let, thus creating a local scope for spl.
But this is only a solution when the variable is only needed inside the cell, not outside.

10 Likes

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.