problems implementing protocol (errors not helpful)

35 views
Skip to first unread message

Jordi Polo

unread,
Sep 18, 2016, 3:25:20 AM9/18/16
to elixir-lang-core

I'm writing to the core list because it seems to me a problem with the language.

To teach myself, I've done this silly thing, mostly following the official documentation:

defmodule NewTable do
  defstruct data
: []
 
@type t :: %__MODULE__{
    data
: nonempty_list(any)
 
}
end

defimpl
Enumerable, for: NewTable do
 
def reduce(t, acc, fun) do
   
Enum.reduce(t.data, acc, fun)
 
end
 
def count(t) do
   
{:ok, length(t.data)}
 
end
 
def member?(t, x) do
   
{:ok, Enum.member?(t.data, x)}
 
end
end



I know there is no point of implementing Enumerable for a list but this is just a learning exercise.
count and member work.
The above reduce method does not work. Trying to use Enum.at, Enum.max, etc on that struct will give back several errors about anonymous functions which are not clear at all.
I've tried with current master also, I get a different set of errors and even something I really do not know how to interpret:

Enum.max(%NewTable{ data: [1,2]})
{:cont, {:cont, #Reference<0.0.4.735>}}

I've tried to use  :lists.foldl(fun, acc, t.data) instead of Enum.reduce(t.data, acc, fun) but gets the same kind of error.

I'm thinking this may be a language issue because if the above code is totally broken, it seems to me, reading the docs that it should work, so maybe the docs are misleading? Or maybe I'm just too new and I'm using some old way of thinking?
And in any case the errors provided does not give a hint about the problem


José Valim

unread,
Sep 18, 2016, 4:14:41 AM9/18/16
to elixir-l...@googlegroups.com
The docs are definitely wrong. It should be Enumerable.reduce (or Enumerable.List.reduce) instead of Enum.reduce. Enumerable.reduce is expected to receive and return tagged tuples, opposite to Enum.reduce. Could you please send a pull request or open up an issue with a link to exactly where in the documentation it is wrong.

Thank you for reporting, it is very appreciated!



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5df8b5e1-5d9c-4edc-86e7-0c47e07c50a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages