Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created July 4, 2013 13:46
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 cromwellryan/5927888 to your computer and use it in GitHub Desktop.
Save cromwellryan/5927888 to your computer and use it in GitHub Desktop.
Implementing Enumerable protocol for the Person record not doesn't seem to work. I'm running this setup which recompiles *.ex files to be made available in iex. http://cromwellhaus.com/2013/07/my-learning-elixir-erlang-setup/
defrecord Person, children: []
defimpl Enumerable, for: Person do
def count(person) do
Enum.count(person.children)
end
def member?(person, value) do
person.children.member(value)
end
def reduce(person, acc, fun) do
person.children.reduce(acc, fun)
end
end
** (ArgumentError) argument error
:erlang.apply(["a","b"], :count, [])
/Users/cromwellryan/projects/elixir_001/math.ex:7: Enumerable.Person.count/1
erl_eval.erl:569: :erl_eval.do_apply/6
src/elixir.erl:152: :elixir.eval_forms/3
/private/tmp/elixir-KfjI/elixir-0.9.3/lib/iex/lib/iex/server.ex:81: IEx.Server.eval/4
/private/tmp/elixir-KfjI/elixir-0.9.3/lib/iex/lib/iex/server.ex:37: IEx.Server.do_loop/1
me = Person.new( children: ["Ryan Jr", "Ben", "Lana", "Liam"] )
# Should return 4... I think
Enum.count(me)
@cromwellryan
Copy link
Author

Turns out you need to reload modules explicitly in iex. elixirc will compile the defimpl for a type into many unique .beam files. You don't load those explicitly. To get the compiled updates into iex run

r(ModuleName)

where ModuleName is 'Person'. So to reload Person record and it's protocol implementations

r(Person)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment