Inspecting which modules imlement a Protocol?

Is there a way to see which modules have implemented a protocol? I see a list when there is an error – where does this come from?

This protocol is implemented for the following type(s): DBConnection.Stream, DBConnection.PrepareStream, Timex.Interval, HashSet, Range, Map, Function, List, Stream, Date.Range, HashDict, GenEvent.Stream, MapSet, File.Stream, IO.Stream

For example, is there a help function like implements(Enumerable) which would produce a list? I am looking in Elixir source code but I cannot follow.

Thank you for any help!

Such a list is only available when protocols were consolidated, but then you can use protocol reflection:

https://hexdocs.pm/elixir/Protocol.html#module-reflection

2 Likes

Protocol.extract_impls/2 will do

The example from the documentation:

iex(1)> path = :code.lib_dir(:elixir, :ebin)
iex(2)> mods = Protocol.extract_impls(Enumerable, [path])
[Date.Range, HashDict, Stream, GenEvent.Stream, File.Stream, Function, List,
 Range, HashSet, IO.Stream, Map, MapSet]
iex(3)> List in mods
true