I just watched a video on protocols in Clojure and it explained how ‘multimethods’ work. It seems to me that they look very similar to how extension methods in C# work. Are they basically the same thing (with the exception that you don’t need to create a static class in Clojure) or is there a fundamental difference? Is there an advantage or disadvantage in using either?
I just watched a video on protocols in Clojure and it explained how ‘multimethods’
Share
The mutlimethod feature in Clojure is for multiple dispatch scenarios. It effectively enables runtime polymorphism where the method that is invoked depends on the type of the arguments to the method (traditional single-dispatch polymorphism depends on the runtime type of the object receiving the method call). Basically, you can think of single-dispatch polymorphism as a method
Mand the actual method that is invoked depends on the runtime type of
arg1(so we’re rewriting the usual syntax ofas
to make the analogy clear. In multiple dispatch, the method that is invoked by
depends on the runtime types of
arg1,arg2, …,argnas well.You can achieve similar functionality in C# with
dynamic.Frankly, it’s not related to extension methods at all.