Hi Kenny,

Unfortunately, I think that macro will not work in all the scenarios :( For 
example:

iex_type = if condition?, do: "foo", else: "bar"
import_with_interpolation ".iex.#{iex_type}.exs"

For you use case, I think you could use a similar macro:

defmacro import_iex_type_file do
  iex_type = if condition?, do: "foo", else: "bar"
  iex_type_file = ".iex.#{iex_type}.exs"

  quote do
    import_file_if_available unquote(iex_type_file)
  end
end

Hope that helps :)


On Monday, February 10, 2020 at 9:05:59 PM UTC+1, Kenny Evitt wrote:
>
> I recently discovered the wonders of `.iex.exs` files!
>
> But my colleague pointed out that we might want some code, e.g. `import 
> X`, to run only in specific environments. In particular we wanted some 
> code, specifically an `import X`, to NOT run in `:prod`.
>
> I created a post on Elixir Forum about this:
>
>  - Environment-specific `import` in (project) `.iex.exs` file? - 
> Questions / Help - Elixir Forum 
> <https://elixirforum.com/t/environment-specific-import-in-project-iex-exs-file/28920>
>
> I ended-up with a solution that uses a macro to wrap calls to 
> `import_if_available` so that an interpolated string could be used:
>
> ```elixir
>   defmacro import_with_interpolation(t) do
>     t_string      = Macro.to_string(t)
>     {t_evaled, _} = Code.eval_string(t_string)
>
>     quote do
>       import_file_if_available unquote(t_evaled)
>     end
>   end
> ```
>
> It seems like it would be easy enough to just update the existing 
> `import_if_available`. Or maybe my macro could be added to `IEx.Helpers` 
> separately.
>
> Thoughts? (Problems with my macro code?)
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/2d7bce36-e612-4e0d-bb2e-55390cbab04f%40googlegroups.com.

Reply via email to