Making "@fallback_to_any true" more flexible

29 views
Skip to first unread message

Mário Guimarães

unread,
Feb 27, 2019, 8:00:26 AM2/27/19
to elixir-lang-core
Hello,

I was trying to make a default implementation of a protocol via

defprotocol TheProtocol do
   
def fun1(...)
   
def fun2(...)

end

defimpl
TheProtocol, for: Any do
   
def fun1(...) do
   
end
   
def fun2(...) do
   
end
end


where "TheProtocol" defines several functions.

Then I was trying

defimpl TheProtocol, for: Atom do
   
def fun1(...) do
     
...
   
end
end


and expecting that when calling

TheProtocol.fun2(an_atom, ...)

it would invoke the implementation in Any, but instead it complains with a warning that a fun2 is required but not implemented for Atom.

Is it possible to change @fallback_to_true so that it leverages the functions from Any when these are not implemented in another implementation?

To what I am aware of, this should not have semantic effects on existing applications, because these are providing complete protocol implementations.

Thanks
Mário

José Valim

unread,
Feb 27, 2019, 12:44:40 PM2/27/19
to elixir-l...@googlegroups.com
Protocols do not provide default implementations for functions on purpose, as that would make it easier for developers to write large protocols, while they should attempt to provide protocols that are as minimal as possible, or break large ones into small ones.

So in your case, you can use defdelegate to delegate where you want to (in this case, the Any implementation).


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
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/eab96901-dc5c-41e8-84bb-e23bda2c2d6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mário Guimarães

unread,
Feb 27, 2019, 4:19:38 PM2/27/19
to elixir-lang-core
Hi José,

the same reasoning would be applicable against having large modules and functions, thought no similar restriction exists for these.

There must be then some strong reason for making it hard to have large protocols, perhaps of technical one or of another kind, which is not clear.

Anyway, thanks for your response.
Mário Guimarães

José Valim

unread,
Feb 27, 2019, 4:24:28 PM2/27/19
to elixir-l...@googlegroups.com
the same reasoning would be applicable against having large modules and functions, thought no similar restriction exists for these.

Yes. It also applies to modules and functions and Elixir does not have any built-in facility to *promote* large modules and functions. As it shouldn't have anything to promote something similar for protocols. It is doable in both cases,  but it is up to you to do it.
 
Reply all
Reply to author
Forward
0 new messages