eksperimental

eksperimental

Conditional import and lexical scope

Is there a way to conditionally import a function, but make the import available outside the if or case?

this obviously works:

  def foo() do
    import Enum, only: [map: 2]

    map(1..5, &IO.inspect(&1))
  end

this works as well:

    if is_integer(term) do
      import Enum, only: [map: 2]
      map(1..term, &IO.inspect(&1))
    end

this does not work due to lexical scope.

    if true do
      import Enum, only: [map: 2]
    end
  
    map(1..integer, &IO.inspect(&1))

So far so good.
My question is, how can we import conditionally at runtime, and make the import available outside the condition clause.

The only solution that I thought it could make it work was this, it does not and it baffles me.:

    true && (import Enum, only: [map: 2])
    map(1..integer, &IO.inspect(&1))

Note: for my use case I can solve this by running the condition at compile time, but I am just trying to understand how to do it at runtime.

Thank you

Marked As Solved

al2o3cr

al2o3cr

The behavior makes sense if you look at the implementation:

The right-hand expression in && winds up inside a case statement, so the import does not leak out.

Also Liked

LostKobrakai

LostKobrakai

That’s what I’d do, which seems a lot cleaner than conditionally importing things.

mod = if condition, do: Stream, else: Enum
mod.map(1..integer, &IO.inspect(&1))
ityonemo

ityonemo

Imports are compile-time.

dimitarvp

dimitarvp

Elixir being FP I don’t see any other way except closures or just passing a function like so

defmodule Helper
  if compile_time_condition do
    def with_scope(fun) do
      import Enum
      fun.()
    end
  else
    def with_scope(fun) do
      import Stream
      fun.()
    end
  end
end

Or you can do it with a keyword list parameter with :do and :else like the if macro does.

Where Next?

Popular in Questions Top

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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement