Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir error "erlang error: :guard_expr"

Tags:

elixir

On the iex console, I found the following error that I'm unsure what I've done wrong...

case %{} do
  x when x == %{} -> true
  _x -> false
end

Results in the following error:

** (ErlangError) erlang error: :guard_expr

Also, I wanted to explain how I found this. I attempted to make my own || macro by looking at elixir's implementation and changing it to treat [], {}, and %{} the same as false and nil. https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel.ex#L2313

Here's that implementation (it has the same problem):

defmodule Or do
  defmacro left || right do
    quote do
      case unquote(left) do
        x when x in [false, nil] or x == [] or x == {} or x == %{} ->
          unquote(right)
        x ->
          x
      end
    end
  end
end

Taking away the or x == %{} makes things work.

like image 890
mgwidmann Avatar asked Dec 15 '14 20:12

mgwidmann


1 Answers

This appears to be an Erlang bug. Here is the erlang code to reproduce the issue:

case #{} of X when X == #{} -> X end.

Thanks to everyone for confirming! See comments for more details.

like image 164
mgwidmann Avatar answered Oct 24 '22 09:10

mgwidmann