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

  • SEARCH
  • Home
  • 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 7729061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:52:29+00:00 2026-06-01T05:52:29+00:00

Having recently seen a presentation of Clojure Protocols, I was quite impressed by the

  • 0

Having recently seen a presentation of Clojure Protocols, I was quite impressed by the clean way extensions to existing types can be done this way.
However, I was pretty sure to have already seen a similar way of doing this in some other language and after some time I found out it was Groovy Categories.

Compare this:

 @Category(String) ​class StringCategory {
   String lower() {
     return this.toLowerCase()
   }
 }

 use (StringCategory) {
   println("TeSt".lower())
   assert "test" == "TeSt".lower()
 }

to the Clojure Protocol equivalent (taken from mikera’s answer below and tested in ideone.com)

 (defprotocol Lowerable
   (lower [x]))

 (extend-protocol Lowerable
   String
     (lower [s] 
       (.toLowerCase s)))

 (println (lower "HELLO"))

my question is:

  1. besides from performance differences (it is said that Clojure is highly optimized in this regard) – is there a semantic difference between the two approaches?
  2. besides the clumsy syntax, is there anything logically wrong with the Groovy approach?

Disclaimer: I am a complete Clojure newbie!

  • 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-06-01T05:52:30+00:00Added an answer on June 1, 2026 at 5:52 am

    Here’s the rough equivalent Clojure code using protocols:

    (defprotocol Lowerable
      (lower [x]))
    
    (extend-protocol Lowerable
      String
        (lower [s] 
          (.toLowerCase s)))
    
    (lower "HELLO")
    => "hello"
    

    The key distinctions to note about Clojure protocols (which I believe make it distinctive from the Groovy categories version)

    • The Clojure protocol definition does not contain any implementation (it’s more like an interface in this respect). The implementation is provided separately: you can extend the Lowerable protocol to as many different classes as you like without needing to make any alterations to either the classes themselves or the protocol definition. For example, you could define lower to work on a Rope.
    • Your Groovy category above is specialised for Strings – this is not the case with Clojure protocols. In this example, the Clojure protocol “Lowerable” is defined without saying anything about the type of arguments.
    • lower is a proper first class function. So you can use it to build higher order abstractions (via higher order functions) that in turn will accept any arguments to which the Lowerable protocol has been extended.
    • Clojure protocols are heavily optimised since they are designed to exploit the fast method dispatch of the JVM. They therefore get compiled down to very efficient code (there is no dynamic object examination or reflection required)

    Clojure protocols are actually a fairly unique solution to the Expression Problem (linked video is pretty interesting). I think the closest equivalent to Clojure protocols in another language is actually Haskell type classes. Even then it’s a bit of a stretch since Haskell is statically typed and Clojure is dynamically typed….

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

Sidebar

Related Questions

Having recently learned of the DebuggerDisplay attribute, I've found it quite useful. However, one
Having recently bought a Windows Phone I can say the one feature I sorely
Having recently protected my site the best I can against XSS I am now
Having recently read Growing OO systems guided by tests, I am very impressed with
Having recently discovered this method of development, I'm finding it a rather nice methodology.
Having recently gotten into test driven development I am using the Nunit test runner
Having recently introduced an overload of a method the application started to fail. Finally
Having recently discovered design patterns, and having acquired the excellent Head First Design Patterns
This is a weird problem I have started having recently. My team is developing
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.

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.