How to implement Enumerable protocol for struct?

Sorry because many other people asked a similar question… but how do you implement the Enumerable protocol for a struct? I see the docs on Enumerable — Elixir v1.12.0-dev but I do not follow how to actually do this… is there an example somewhere? Just to be able to treat my structs the same as maps for many Enum operations. Thank you!

1 Like

Why not just use Map.from_struct/1?

a = %MyStruct{foo: 0, bar: 1}
a
|> Map.from_struct()
|> Enum.count()
3 Likes

The best example is the source code itself.

But note that you cannot rely on the implementation for maps. The only function that will work is member?/2

2 reasons I cannot use from_struct:

  1. the code that calls this is in 3rd party package – I cannot pre-change my struct
  2. academic. I want to know how to do this – all the posts I see in the forum seem to be how to avoid doing this

Speaking totally naively, it seems like you could start with the implementation for maps, and then just add a step for each callback that addresses how the {:__struct__, ModuleName} key/value pair that is a part of every struct is handled. I can imagine cases where you might want to ignore it, and other cases where you would not.

I would not be surprised if that is wrong/missing something, but that is where I would start and maybe others can speak to why that would/wouldn’t be a good idea.

Thank you all for the suggestions! @eksperimental this source code was very helpful and @srowley you are right some decisions must be taken about this :__struct__ key.

For example I tried implementing this with DateTime struct – I found this is almost 100% copy of the source code for Map impl. The only change I tried was in the count function but I understand this may be different depends on what you need to do:

defimpl Enumerable, for: DateTime do
  def count(map) do
    {:ok, map |> Map.from_struct() |> map_size()}
  end

  def member?(map, {key, value}) do
    {:ok, match?(%{^key => ^value}, map)}
  end

  def member?(_map, _other) do
    {:ok, false}
  end

  def slice(map) do
    size = map_size(map)
    {:ok, size, &Enumerable.List.slice(:maps.to_list(map), &1, &2, size)}
  end

  def reduce(map, acc, fun) do
    Enumerable.List.reduce(:maps.to_list(map), acc, fun)
  end
end

This one is wrong, as the result of size will include the :__struct__ key

Actually my case works even if I return {:error, "no slice"} for this function so I think this means that whatever Enum functions are being used they are not requiring the slice function. I am not sure if this size variable is like the size of “chunk” for getting part of a list? I think maybe sometimes you might want this to include __struct__ key but It most probably depends on the use case. Anyhow I have now at least something that I can play around with. Thank you again!

I don’t understand this part - if you’re passing a struct where the third-party code expects an Enumerable, why couldn’t you just… not do that?

1 Like

Nope. That is not how it works. It is explained in the documentation of each funtion.

Exactly, that’s why I wonder why would you want to implement the Enumerable for DateTime when you are just gonna treat it like a map. You’ d better off convert it to a map.

Hmm… all of your comments make me inspect refactoring more seriously. Maybe my solution is improper. Thank you for your guidance! I am still learning!

1 Like
  1. the code that calls this is in 3rd party package - I cannot pre-change my struct

I would just echo what the others have said–there might be a way to grab the needed data out of your structs before passing it to the other library. Typically you don’t want to implement protocols for data types for which the protocol doesn’t really make sense (what does it mean to enumerate a datetime?).

  1. academic. I want to know how to do this

I think the code that really made things click for me was the Enumable implementation for Range.

I also tried writing a blog post about implementing Enumerable for custom data structures, but I don’t know if it’s any clearer.

1 Like