Can a case be made to have default implementations be delegated to Any
when its implementation is missing?
defprotocol Duration do
@fallback_to_any true
def seconds(a)
def compare(a, b)
end
defimpl Duration for: Any do
def seconds(_), do: raise "Not yet implemented"
def compare(_, _), do: raise "Not yet implemented"
end
defimpl Duration for: NaiveDateTime do
def compare(a, b) do
NaiveDateTime.compare(a, b)
end
# we could implement `seconds` here in the meaning of epoch; but we could also skip it's implementation if we decide there is no semantic need for this.
end
The fact we have @fallback_to_any true
means that it is not unreasonable to expect that missing implementations are defaulted to Any
; so still explicit. Currently a warning is rendered for missing implementations; so the above fallback would get rid of the warning while clearly communicating that its implementation has been omitted deliberately(which could have several reasons, among which is because the domain does not require its implementation.)
Cons to the above suggestion:
- Backwards compatibility of the language(This thread is like 6 years old, lol)
- Protocol should always be fully implementable. If stuff remains omitted then this means the protocol is too broad and it should be made smaller. Counter argument: Naming things is hard and sometimes it is not worthwhile to split up existing protocols in more granular ones.