Skip to content

Instantly share code, notes, and snippets.

@zsoldosp
Created February 8, 2014 19:38
Show Gist options
  • Save zsoldosp/8889000 to your computer and use it in GitHub Desktop.
Save zsoldosp/8889000 to your computer and use it in GitHub Desktop.
Different errors given for the same elixir code - different scoping between iex and elixir? If so, why?
$ elixir macros-why-error.exs
false
** (UndefinedFunctionError) undefined function: MyMacro.unless/2
MyMacro.unless(false, [do: :ok])
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2
(elixir) lib/code.ex:301: Code.require_file/2
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule MyMacro do
...(1)> defmacro unless(clause, options) do
...(1)> quote do: if(!unquote(clause), unquote(options))
...(1)> end
...(1)> end
{:module, MyMacro,
<<70, 79, 82, 49, 0, 0, 7, 244, 66, 69, 65, 77, 65, 116, 111, 109, 0, 0, 0, 120, 0, 0, 0, 13, 14, 69, 108, 105, 120, 105, 114, 46, 77, 121, 77, 97, 99, 114, 111, 8, 95, 95, 105, 110, 102, 111, 95, 95, 4, 100, ...>>,
{:unless, 2}}
iex(2)>
nil
iex(3)> MyMacro.unless false, do: IO.puts "false"
** (CompileError) iex:3: you must require MyMacro before invoking the macro MyMacro.unless/2
(elixir) src/elixir_dispatch.erl:95: :elixir_dispatch.dispatch_require/6
(elixir) src/elixir.erl:150: :elixir.quoted_to_erl/3
(elixir) src/elixir.erl:134: :elixir.eval_forms/4
defmodule MyMacro do
defmacro unless(clause, options) do
quote do: if(!unquote(clause), unquote(options))
end
end
MyMacro.unless false, do: IO.puts "false"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment