Implementing Enum for Explorer.Series

I’m working on a data plotting library for Elixir. I don’t want to force people to use Explorer if they don’t want to, especially because for the actual visualization I have to build normal Elixir structures which represent the state and the geometric figures I want to draw. This means I want to accept sometning like plot([1,2,3], [1,2,3]).

However, I’d like to make it very easy to use series as data sources, and the easiest way of being able to use both series and lists is to implement the Enum protocol for series (basically by calling the already existing Series.to_enum/1 function.

Is this ok? This is my first time implementing a protocol for a non-trivial structure not written by me, and because protocol implementations are global, I wonder if that would be a problem. Should I wrap it in something custom such as %DataSource{} and then implement the %DataSource{} protocol for lists, streams and whatever makes sense?

Given Explorer doesn’t implement the protocol I’d argue you shouldn’t do so either. There’s a warning about to_enum being an expensive operation, so hiding it being called is imo not a great idea.

My take would be just expecting an enumerable and letting users call Series.to_enum on their own. It’s not like that additional function would make using your library more difficult and the explicitness of the call being present in users code is imo justified due to the importance for performance.

1 Like