Created
July 4, 2013 13:46
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
** (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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
me = Person.new( children: ["Ryan Jr", "Ben", "Lana", "Liam"] ) | |
# Should return 4... I think | |
Enum.count(me) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
where ModuleName is 'Person'. So to reload Person record and it's protocol implementations