Skip to content

Instantly share code, notes, and snippets.

@ityonemo
Created August 6, 2022 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ityonemo/c6a54b54729bf09c993f72689d7ccdbe to your computer and use it in GitHub Desktop.
Save ityonemo/c6a54b54729bf09c993f72689d7ccdbe to your computer and use it in GitHub Desktop.
matching on attributes
defmodule T do
@foo [%{foo: "bar"}]
def t(@foo) do
:ok
end
end
# DISASSEMBLED RESULT:
#{:function, :t, 1, 9,
# [
# {:line, 1},
# {:label, 8},
# {:func_info, {:atom, T}, {:atom, :t}, 1},
# {:label, 9},
# {:test, :is_nonempty_list, {:f, 8}, [x: 0]},
# {:get_list, {:x, 0}, {:x, 1}, {:x, 2}},
# {:test, :is_map, {:f, 8}, [x: 1]},
# {:get_map_elements, {:f, 8}, {:x, 1}, {:list, [atom: :foo, x: 1]}},
# {:test, :is_eq_exact, {:f, 8}, [x: 1, literal: "bar"]},
# {:test, :is_nil, {:f, 8}, [x: 2]},
# {:move, {:atom, :ok}, {:x, 0}},
# :return
# ]},
# EXPECTED DISASSEMBLY:
# {:function, :t, 1, 9,
# [
# {:line, 1},
# {:label, 8},
# {:func_info, {:atom, T}, {:atom, :t}, 1},
# {:label, 9},
# {:test, :is_eq_exact, {:f, 8}, [x: 0, literal: [%{foo: "bar"}]]},
# {:move, {:atom, :ok}, {:x, 0}},
# :return
# ]}
# COMPARE TO: `@foo ["bar"]`
# {:function, :t, 1, 9,
# [
# {:line, 1},
# {:label, 8},
# {:func_info, {:atom, T}, {:atom, :t}, 1},
# {:label, 9},
# {:test, :is_eq_exact, {:f, 8}, [x: 0, literal: ["bar"]]},
# {:move, {:atom, :ok}, {:x, 0}},
# :return
# ]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment