I was wondering whether is it possible to find all modules that have implementations for certain module?
I have a simple protocol:
defprotocol Ep.PerformTest do
@doc "Should return tupple {out, time}"
def test(struct)
end
And 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
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]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With