Skip to content

Support delayed evaluation of code in .iex.exs #3328

@alco

Description

@alco

It is not currently possible to define a module in .iex.exs and import it at once.

defmodule MyIExHelpers do
  def s do :init.stop end
end

import MyIExHelpers


λ iex
...
Error while evaluating: /Users/alco/tmp/.iex.exs
** (CompileError) .iex.exs:5: module MyIExHelpers is not loaded but was defined. This happens because you are trying to use a module in the same context it is defined. Try defining the module outside the context that requires it.
    (elixir) src/elixir_exp.erl:123: :elixir_exp.expand/2
    (stdlib) lists.erl:1352: :lists.mapfoldl/3
    (stdlib) lists.erl:1353: :lists.mapfoldl/3
    (elixir) src/elixir_exp.erl:49: :elixir_exp.expand/2

Even if the module definition is extracted into a separate file, it doesn't work:

Code.require_file "my_iex_helpers.exs"
import MyIExHelpers

λ iex
...
Error while evaluating: /Users/alco/tmp/.iex.exs
** (CompileError) .iex.exs:2: module MyIExHelpers is not loaded and could not be found
    (elixir) src/elixir_exp.erl:123: :elixir_exp.expand/2
    (stdlib) lists.erl:1352: :lists.mapfoldl/3
    (stdlib) lists.erl:1353: :lists.mapfoldl/3
    (elixir) src/elixir_exp.erl:49: :elixir_exp.expand/2

A suggested workaround for this would be the ability to return a quoted expression from .iex.exs which would then be evaluated in the IEx process and therefore in a different context from the one the module has been defined in.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions