vgrechin

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

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

josevalim

Creator of Elixir

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

josevalim

Creator of Elixir

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

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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement