This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-13
Channels
- # announcements (1)
- # babashka (12)
- # beginners (10)
- # biff (9)
- # calva (2)
- # cherry (21)
- # cider (14)
- # clj-commons (76)
- # clj-kondo (8)
- # clj-on-windows (34)
- # cljs-dev (5)
- # clojure (48)
- # clojure-austin (7)
- # clojure-europe (97)
- # clojure-nl (1)
- # clojure-norway (14)
- # clojure-uk (22)
- # clojurescript (137)
- # conjure (33)
- # cursive (4)
- # datalevin (1)
- # deps-new (4)
- # devcards (2)
- # duct (3)
- # events (1)
- # fulcro (12)
- # graphql (9)
- # hyperfiddle (16)
- # jobs (8)
- # kaocha (1)
- # leiningen (6)
- # lsp (39)
- # malli (38)
- # membrane (20)
- # nbb (68)
- # observability (7)
- # off-topic (49)
- # pathom (11)
- # polylith (8)
- # portal (22)
- # re-frame (6)
- # releases (1)
- # remote-jobs (2)
- # shadow-cljs (24)
- # spacemacs (2)
- # squint (6)
- # xtdb (7)
Hi, everyone. I’m doing koans 27, and trying to understand how to define a multi that takes 2 args.
I keep getting an error - Wrong number of args (2)
, even though, as far as I can tell,
my method is syntactically identical to the even-or-odd method that I took from StackOverflow, which does work:
;; Mine - doesn't work
(def user1 {:name "Elior" :contact-method "sms"})
(def user2 {:name "Lily" :contact-method "email"})
(defn sms-user[user u]
(println
(str (:name user) "smsing " (:name u))
))
(defmulti notify-user (fn [user _] (:contact-method user)))
(defmethod notify-user "sms" [a _] (str a _))
(notify-user user1 user2)
;; StackOverflow - works
(defmulti even-or-odd (fn [x _] (even? x)))
(defmethod even-or-odd true [a _] (str a _))
(defmethod even-or-odd false [a _] :odd)
(even-or-odd 2 3)
Are you testing this at the REPL? defmulti
doesn't allow you to redefine the dispatch function when it's inline. There's a helpful example here: https://clojuredocs.org/clojure.core/defmulti#example-55d9e498e4b0831e02cddf1b
(notify-user user1 user2)
=> "{:name \"Elior\", :contact-method \"sms\"}{:name \"Lily\", :contact-method \"email\"}"
I suspect as well that what you're seeing is some "dirty" REPL state. You can easily check if this is the case by firing up a new REPL session - you should see the same results as I did when evaluating the code in the new session.
I just found this, maybe you have the same problem: https://clojureverse.org/t/multimethods-wrong-number-of-args-2-passed-to/5796 and https://groups.google.com/g/clojure/c/XcsouTQeLxw
Thanks, you’re right, it does work in a new REPL. Which begs the question - when does a REPL get “dirty”?
That wasn't REPL issue, but defmulti
behaviour mentioned by @UE72GJS7J (my links describe that as well).
Are there any other functions that behave similarly? When extending protocols, maybe? :thinking_face:
@U040HQSN3Q9 protocols don’t display this gotcha. I don’t want to claim that there’s nothing else that behaves this way, but I can’t think of any, and defmulti is the one that predictably causes a lot of pain : )