Problem: use Code.eval_string in compile-time

defmodule Q do
  stems = 
    File.read!("stems")
    |> String.split("\n", trim: true)
    |> Enum.reduce(%{}, fn x, map -> 
      d = Code.eval_string(x) |> elem(0)
      Map.put(map, elem(d, 0), elem(d, 1))
    end)
  def stems_data(), do: unquote(Macro.escape(stems))
end

stems file example

{"3-d", ["3-d reconstruction"]}
{"abiot", ["abiotic"]}
{"abnorm", ["chromosome abnormality"]}
{"acetyl", ["acetylation", "acetyltransferases"]}
{"acetyltransferas", ["acetyltransferases"]}

It seems that Code.eval_string does not work in compile-time.
with

** (CompileError) nofile:1: undefined function .../0
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1355: :lists.mapfoldl/3

why?
Is there a way to replace that line with other code?

remark: I am using elixir 1.5.1, OTP 20.0

It should work fine at compile-time (that is its major usage). That error indicates that there is an unquoted ... in your file though, so your file does not seem to be valid expressions-per-line.

Thanks, I just forgot limit: :infinity in IO.inspect

1 Like

I’m not seeing any IO.inspect calls in your code? o.O

just in the code generating the file

Oh you are exporting it then reading it in! Ah yeah you did not show that part and your example reproduced fine with the code you showed. :slight_smile:

It is always good to give a complete reproducible example that demonstrates the problem. :slight_smile:

agree :smile:

1 Like