Is it possible to find all modules that implement a particular protocol?

I was wondering if it is possible to find all the modules that have implementations for a particular module?

I have a simple protocol:

defprotocol Ep.PerformTest do @doc "Should return tupple {out, time}" def test(struct) end 

And a few modules that have implementations of this protocol:

 defmodule Ep.Test.Rexcpp do defstruct [:input, :code, :output] def displayName(), do: "Rextester C++" defimpl Ep.PerformTest, for: Ep.Test.Rexcpp do def test(struct) do end end end 
+7
elixir protocols
source share
1 answer

Protocol.extract_impls / 2 seems to be what you are looking for.

Retrieves all types implemented for this protocol from the specified paths.

Thanks to the OP comment, here is what code should look like an example in a question:

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

Since we call Erlang :code here to get the path, the module name must be in the Erlang atom format used.

+7
source share

All Articles