Methods in Clojure
Clojure supports functions defined on record types, which are similar to methods on structs in other languages.
To run this program, save it as methods_example.clj
and use the Clojure command-line tool:
In this Clojure example, we define a Rectangle
record type using defrecord
. Instead of methods, we define functions that take the record as their first argument. This is a common pattern in Clojure and other functional programming languages.
The area
and perim
functions are defined outside the record, but they operate on Rectangle
instances. This is similar to how methods work in other languages, but with a more explicit passing of the record as an argument.
In the -main
function, we create a Rectangle
instance and call our functions on it. Clojure doesn’t have the concept of pointers vs values in the same way as some other languages. All data structures in Clojure are immutable by default, so we don’t need to worry about whether we’re passing a reference or a value.
Next, we’ll look at Clojure’s mechanism for defining abstract behaviors: protocols and multimethods.