vgrechin
How do I redifine modules in livebook?
I redefine a module in my livebook for educational purpose in order to demonstrate evolution of code within the same page.
I migrating the notebook to v0.9.2 and I encounter an error. How can I now redefine the module within the same page of Livebook?
** (CompileError) notebook/MyNotebooks/Notebook1.livemd#cell:xb7luhdi543e3ttejayalzbnuf3crtgd:1: module MyModule is already defined
Most Liked
Adzz
I ran into this problem too. It makes it a nightmare to write tutorials and docs honestly because you can’t incrementally add things to the a module, you have to keep renaming it. It’s also different from how IEX works which is surprising…
josevalim
That’s exactly the issue. Module names are global, so how can you make only subsequent cells see a given module definition? Even if you say: “well, we will purge and swap module versions before execution”, you can still have long-running processes and other functionality that will now be swapping modules on the fly.
Redefining a module is global mutable state and, if you want reproducible notebooks, then it won’t work.
benwilson512
A work around could be to do like:
defmodule Foo0 do
end
alias Foo0, as: Foo
And then later define a Foo1 and alias Foo1 as Foo.
Then all your code would of course be calling Foo.
josevalim
We don’t plan to allow this because it completely messes up with Livebook ability of writing reproducible code.
If you redefine a module, and evaluate a previous cell, it gets the result of the new module, and now all of your computations may get out of sync.
linusdm
Just to be clear: a failure when redefining a module is intended behaviour since Livebook 0.8.0 (see the changelog about “Improved reproducibility of module definitions”). That’s what you’re seeing. You previously were running on an older version of Livebook that was ok with redefining modules (with the problem of not being fully reproducible, as José mentioned) which is why you had no problems back then. Nothing in Elixir itself changed in any way, regarding this. Livebook is more restrictive to make sure all livebooks are reproducible (run in the same way, no matter in which order you execute the cells). Regular Elixir allows you to redefine modules (it emits a warning if you do) afaik.







