Is it possible to find all modules that implement certain protocol?

Protocol.extract_impls/2 appears to be what you're looking for.

Extracts all types implemented for the given protocol from the given paths.

Thanks to OP's comment, here's what the code should look like for the example in the question:

path = :code.lib_dir(:protocol_test, :ebin)
mods = Protocol.extract_impls(Ep.PerformTest, [path])

Since we're calling the Erlang :code module here to get the path, the module name needs to be in the atom format Erlang uses.


You're looking for the __protocol__/1 method. From the docs:

__protocol__/1 - returns the protocol information. The function takes one of the following atoms:

  • :impls - if consolidated, returns {:consolidated, modules} with the list of modules implementing the protocol, otherwise :not_consolidated

  • [...]


Example:

iex> String.Chars.__protocol__(:impls)
# => {:consolidated, [Postgrex.Copy, Postgrex.Query, Decimal, Float, DateTime, Time, List, Version.Requirement, Atom, Integer, Version, Date, BitString, NaiveDateTime, URI]}