I am quite a fan of functional programming, I do know about pattern matching and multiple dispatch, and that's how I found about predicate dispatch and was wondering ever since whenever or not it has something to do with pattern matching.
I did read this SO thread: What is Predicate Dispatch but still couldn't get my answer regarding the relation between pattern matching and predicate dispatch. I believe that pattern matching and predicate dispatch are indeed similar, if not equivalent, but would like to hear a few opinions.
Consider this Elixir code:
def function(%{a_flag: True}=struct) do
# do smth
end
def function(struct) when is_nil(struct.field) do
# do smth else
end
def function(struct) do
# default case
end
It does look like the function is picked at runtime based on the properties of the input argument(s), which is exactly what predicate dispatch is. The when
part looks similar to how predicate dispatch is proposed in this article: http://web.cs.ucla.edu/~todd/research/oopsla04.pdf
Please share your knowledge/opinion on this matter.