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 7849985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:42:25+00:00 2026-06-02T18:42:25+00:00

I have some code that uses multi-methods and would ideally like to overload the

  • 0

I have some code that uses multi-methods and would ideally like to overload the function (in this case, multi-function) so that I can pass in a higher order function to help with testing, for example.

Here’s the example:

(ns multi)

(defn my-print [m] (println "The colour is" (:colour m)))

(defmulti which-colour-mm (fn [m f] (:colour m)))

(defmethod which-colour-mm :blue [m f] (f m))
(defmethod which-colour-mm :red [m f] (f m))
(defmethod which-colour-mm :default [m f] (println "Default: Neither Blue nor Red"))

(defn which-colour
  ([m] (which-colour-mm m my-print))
  ([m f] (which-colour-mm m f)))

(which-colour {:colour :blue :object :ball})
(which-colour {:colour :yellow :object :ball})
(which-colour {:colour :blue :animal :parrot} (fn [m] (println "The " (:animal m) "is" (:colour m))))

So my defn provides the arity overloading but I’m wondering if defmethod supports anything like this. (I guess you wouldn’t want to do it for each defmethod declaration.)

Is this the most suitable (dare I say, idiomatic) approach, or is there a better way?

  • 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-02T18:42:27+00:00Added an answer on June 2, 2026 at 6:42 pm

    This is perfectly fine. There is the “user” interface and the “type” interface of a library. They may be identical, but they don’t have to.

    The “user” interface is in your case which-colour. The “type” interface is which-colour-mm (ok, not really, but just for the sake of the argument). The user of your library does not need to know about the multimethod.

    On the other hand someone providing a new colour – say :purple – does not have to care about multi-arity boilerplate. This is handled for him in which-colour.

    This is a perfectly valid design!

    But of course there’s a price tag: Suppose you have a colour, which has some more perfomant way to do things… Now, you are locked into a possible slower interface.

    To clarify this a little: Suppose you have a collection interface. You provide a function – conj – which allows the user to add elements to the collection. It is implemented like this:

    (defn conj
      [coll & elements]
      (reduce conj1 coll elements))
    

    conj1 is the “type” interface (eg. a multimethod or protocol function): it adds one element to the collection. So someone supplying a new collection type has only to implement the simple case of adding a single argument. And automagically the new type will also support adding multiple elements.

    But now suppose you have a collection type, which allows a faster way to add several elements than just adding one after the other. This capability cannot be used now.

    So you make the multimethod/protocol function the function conj itself. Now the collection can use the faster way. But each implementation must provide the multiple elements boilerplate.

    This is a trade-off and up to your decision. There is not Right Way(tm). Both can be considered idiomatic. (Although I personally would try to go with the first one as often as possible.)

    YMMV.

    Edit: An example of multi arity methods without coding in the dispatch value.

    (defmulti which-colour-mm (fn [m & args] (:colour m)))
    (defmethod which-colour-mm :blue
      ([m] (print m))
      ([m f] (f m)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses the Oracle function add_months to increment a Date
We have some code kicking around that uses this old internal Sun package for
I have some code that uses a multi-part form and HTML5 file object to
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
I have some code that currently uses a ST monad for evaluation. I like
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that uses Open XML to open up a .docx file,
I have some code that uses ODP.Net using (OracleConnection connection = new OracleConnection(connectionString)) {
I have some Java code that uses curly braces in two ways // Curly
I have some old code that uses qsort to sort an MFC CArray of

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.