Quick OOP Example

Want some Object Oriented Programming in Clojure? Here’s a quick example.

Let’s create a dog object.

Now let’s use the dog object.

See, OOP in Clojure is fairly simple. This sample uses protocols with records, but you can add polymorphism with just a little extra work. For polymorphism, use records and multimethods.

Razvan’s ‘defrecord’ Example

I absolutely love the user razvan’s Clojure defrecord example over at stackoverflow, so I copied it here. I know I’ll never find it again if I don’t. Look at the original over on stackoverflow at this link.

Here’s the example …

Clojure defrecord example:

If you’re looking for more information on combining protocols with records in Clojure, then Matthew Boston has the blog for you!