Accessing the module name that implements a protocol

41 views
Skip to first unread message

eksperimental

unread,
Jan 13, 2021, 10:38:34 PM1/13/21
to elixir-l...@googlegroups.com
I am working on a protocol, and I need to have access to the module
that implements the protocol. For example, `Enumerable.List`.

It is actually mentioned in the documentation, but I would like to know
whether this is an implementation detail or a non-documented standard
that I can rely on. In case it is an implementation detail, a function
like Protocol.impl(protocol_module, implementation_module) would be
desired.

iex> Protocol.impl(Enumerable, List)
Enumerable.List

Thank you.

José Valim

unread,
Jan 14, 2021, 3:48:44 AM1/14/21
to elixir-l...@googlegroups.com
You should use `Protocol.impl_for(data)`, which is optimized to build the module without constructing the module name manually (which can be expensive so we want to avoid it).

If you only have the module name for some reason, then it is far to assume it will be Protocol + Impl name.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5fffbcb7.1c69fb81.f59a4.3b1bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.

eksperimental

unread,
Jan 14, 2021, 9:15:00 AM1/14/21
to elixir-l...@googlegroups.com

> If you only have the module name for some reason, then it is far to
> assume it will be Protocol + Impl name.
Do you mean "it is fair" ?

I want to avoid the cost of protocol dispatching by calling the
functions directly,
Thank you

José Valim

unread,
Jan 14, 2021, 9:33:02 AM1/14/21
to elixir-l...@googlegroups.com
The protocols are already optimized to dispatch as efficiently as possible. How can you be sure that, by doing your own protocol dispatch, you won't be doing that protocols do but slower?

In any case, impl_for returns the impl for a given data.

eksperimental

unread,
Jan 14, 2021, 9:02:41 PM1/14/21
to elixir-l...@googlegroups.com
This protocol has some behaviour callbacks, one of them is `empty/0`. So
I cannot do MyProtocol.impl_for(term) because in that callback there is
no term (I am returning a term though).
https://github.com/eksperimental/buildable/blob/a2aa7658e58820472f56e7b1b7e3b3e2293c7950/lib/buildable/delegation.ex#L14

So what are my options for delegating to MyProtocol.MyImplementation ?

José Valim

unread,
Jan 15, 2021, 1:37:31 AM1/15/21
to elixir-l...@googlegroups.com
So it seems you should not be using protocols in the first place and model it with behaviors. Or build the module name by hand as first mentioned. :)

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages