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