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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement