How to use the defined "Any" in protocol?

I don’t understand why “Any” fallbacks don’t work in this case. Here is the story:

I am using Timex library for my models in a phoenix project.

$iex -S mix
# This works
iex()> Timex.is_valid?({2016, 01, 01})
true

# This should be false
iex()> Timex.is_valid?("caturday")
** (Protocol.UndefinedError) protocol Timex.Protocol not implemented for "caturday"
    (timex) lib/protocol.ex:1: Timex.Protocol.impl_for!/1
    (timex) lib/protocol.ex:150: Timex.Protocol.is_valid?/1

Looking at the code for Timex.is_valid? it delegates to Timex.Protocol where the catch All version returns a tuple {:error, :invalid_date}.

So why in the above example I get an exception instead of the tuple?

Edit: I even added a line in my local copy of deps/timex/lib/protocol.ex

 @fallback_to_any true

and recompiled but still doesn’t work

If you define @fallback_to_any true then it should definitely work. Make sure that you modified your dependency, recompiled it and then recompiled your app. One idea is to just delete _build.

2 Likes

Thanks. Deleted _build and it works

1 Like