Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4610224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:06:04+00:00 2026-05-22T01:06:04+00:00

In the book The Joy of Clojure, defprotocol is offered as a solution to

  • 0

In the book “The Joy of Clojure”, defprotocol is offered as a solution to the expression problem — “the desire to implement an existing set of abstract methods for an existing concrete class without having to change the code that defines either.”

The example given is as follows:

(defprotocol Concatenatable
  (cat [this other]))

(extend-type String
  Concatenatable
  (cat [this other]
    (.concat this other)))

(cat "House" " of Leaves")
;=> "House of Leaves"

(extend-type java.util.List
  Concatenatable
  (cat [this other]
    (concat this other)))

(cat [1 2 3] [4 5 6])
;=> (1 2 3 4 5 6)

It is suggested that this is not possible in a language like Java, but how is it different than the following?

public class Util {
  public static String cat(final String first,
                           final String second) {
    return first.concat(second);
  }

  public static <T> List<T> cat(final List<T> first,
                                final List<T> second) {
    final List<T> list = new List<T>(first);
    list.addAll(second);
    return list;
  }
}

After all, both are used similarly:

(cat "House" " of Leaves")
Util.cat("House", " of Leaves");

The Clojure function cat is not a method on the String and List classes, but rather an independent function that is overloaded to accept either String or List arguments.

Although I really like Clojure, I don’t understand the claims of superiority for this construct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:06:04+00:00Added an answer on May 22, 2026 at 1:06 am

    Okay. You release this cat Java library to much fanfare, and everyone downloads it. It’s so great I want to make my own TVCommercial type be concatenable so that I can send it to bits of your library that operate on concatenable objects.

    But I can’t, because you call Util.cat(obj1, obj2), which has no overload for TVCommercial. I can’t extend your code to handle my types, because I don’t own your code.

    You can define Concatenable as an interface to address this problem:

    interface Concatenable {
      Concatenable cat(Concatenable other);
    }
    

    But now I can’t write a class which is both Concatenable and… I don’t know, an AnimalHandler, that handles cats. Clojure’s protocols solve both problems by decentralizing the dispatch functions and implementations: they live all over the place, rather than in some single location. In Java, you choose between:

    • Putting all your type dispatch into a single switch/case or overloaded method
    • Defining an interface mandating a method with a particular name

    Clojure basically does the latter of these, but because it uses namespaced names, there’s no danger of conflict with other protocols that think cat is a good function name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was following The Joy of Clojure and I am puzzled with these 2
Book uses different terms for Linq-to-XML methods/properties defined in classes XObject , XNode ,
Im reading Jon Skeet book. ( Expression Trees Chapter) It has an example of
I have a book entity which has a OneToMany relationship set up to Document
The book that I purchased to help with my SSIS understanding seems to have
What book would you recommend to learn test driven development? Preferrably language agnostic.
A) Book I’m learning from says that if we handle Login.Authenticate event, then we
Q1 Book suggests that before we register new SqlProfileProvider , we should remove any
Each book can have many authors. And each author can author many books. class
The book, Producing Open Source Software , suggests that it's better to have lengthy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.