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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:13:57+00:00 2026-05-27T17:13:57+00:00

I am missing an important point about defmulti and defmethod. I’ve read several books’

  • 0

I am missing an important point about defmulti and defmethod. I’ve read several books’ explanation of defmulti, and I’m still confused.

I want to get a random value depending on whether or not it’s a transaction or an amount like 100.00

I want to call (random-val) and either get back an avail-trans value or random decimal amount. I have experimented with putting the functions in a map, but I get the same value back for avail-trans, a \B.

(def^:dynamic map-val {:trans (random-trans) :amt (random-amount)})

Here is the smallest amount of code to show what I’m doing that is not working. I’d appreciate any pointers or help.

(def^:dynamic avail-trans [\B \W \D \A])

(defn random-trans 
    [] 
    (nth avail-trans (.nextInt random (count avail-trans))))

(defn random-amount
    []
    (float (/ (.nextInt random (count (range 1 10000))) 25 )))

The following is not constructed correctly, but I’m not sure why or how to fix the problem:

(defmulti random-val :val-type)

(defmethod random-val :trans []
    (random-trans))

(defmethod random-val :amt []
    (random-amount))

Calling (random-val :trans) results in this error:

java.lang.IllegalArgumentException: No method in multimethod ‘random-val’ for dispatch value: null (NO_SOURCE_FILE:0)

  • 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-27T17:13:58+00:00Added an answer on May 27, 2026 at 5:13 pm

    A multimethod is created with defmulti; you’re doing that right. defmulti needs a name and a dispatch function (and a docstring, plus some options, if you desire, but forget about those).

    (defmulti random-val identity) 
    

    When you implement the multimethod with defmethod, you need to specify the name of the multimethod you’re implementing, the dispatch value it should match, and then the function tail (arglist plus whatever you want it to do).

    (defmethod random-val :trans [t] (random-trans))
    (defmethod random-val :amt [t] (random-amt))
    

    You’re getting java.lang.IllegalArgumentException: No method in multimethod 'random-val' for dispatch value: null (NO_SOURCE_FILE:0) because when the dispatch function you assigned random-val, :val-type is applied to any other keyword, it gives you null. When Clojure tries to look up a method to match that dispatch value, it fails.

    But even if it didn’t fail there, your defined methods have 0 arity (take no values), so you need to fix that, too (done above).

    Finally, this doesn’t seem like a good use for protocols. Just use your two separate function, random-amount and random-trans.

    Note, too, that Clojure’s website has a good explanations of multimethods.

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

Sidebar

Related Questions

I know I'm missing one important rule about static methods but would would be
There are important features of Emacs which are missing in Vim, such as the
I realize I'm probably just dumb and missing something big and important, but I
I seem to be missing something about LINQ. To me, it looks like it's
I might be missing something obvious but is there a reference somewhere about what
Probably missing something completely obvious here, but here goes. I'm starting out with Spring
I'm missing something here: $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter =
What is missing in the Visual Studio 2008 Express Editions? In particular, what functionality
I might be missing something really obvious. I'm trying to write a custom Panel
in CSS: The missing manual the author says that font-size: medium (or other size

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.