Suppose I have a bunch of Clojure data structures, all of the same type – for example an object type defined by defrecord.
What is the best way to get polymorphic behaviour across these structures?
Would it be good practice to embed a function within the structure so that I can do something like:
((:my-method my-object) param1 param2)
Or is there a better way?
You can use multimethods for dispatch,
then you can dispatch based on type
You need to add :type to the objects metadata when you create it,
or I haven’t tried this but since protocols and records creates java classes behind the scenes you dispatch based on that name.