IEx need to define module


In IEx one has to first define a module in oder to define a function.
Is there a way to do that dynamically? Or extend a defined module in IEx?

I’m thinking about how this works in Lisp or Clojure where you can change the package/namespace using (in-package ) or (in-ns ).
Then you can define whatever function you want without having to re-evaluate the module definition.


The Elixir default shell doesn’t do that, but it would be possible to make one that does. There were some alternative shells for Erlang that did. Usually I’ll just do blah = fn ... end though.

1 Like

This is not possible as far as I know.

I don’t have the insights into the IEx code.

But this feature would IMO be needed to be able to prototype some more serious code.

You might be able do it if you recompiled an aliased “home module” every time you detect “def” being invoked, and do a module reload. I wouldn’t recommend it, though.

Needed? Helpful, for sure. I typically prototype by having an editor window open the module I’m working on and just r Module.Name when required. Works smoothly for me (and I’m still old school on TextMate!!!)

1 Like

Well, if you ask for what is needed, then that is very little.

I’m using Emacs, it certainly works the way you describe it.
But I have to open up the editor, create a file that I later have to delete again, etc.
Quite many steps to just try out some code with a module definition in the REPL.