Hello Clojure land!
I am writing a function that I just realized that I could implement
using multi methods (I think).
However, I will describe the problem anyways.

I want to be able to call a function that will take different actions
depending on the type of in the input.
I first wrote this function.
--
(defn panda [x]
    (case (type x)
        java.lang.String "HELLZ YEAH IT'S A STRING!"
        java.lang.Integer "it's an integer.."))
--
Here is the output of when I test the function.
--
user=> (panda 6)
java.lang.IllegalArgumentException: No matching clause: class
java.lang.Integer
(NO_SOURCE_FILE:0)
user=> (panda "apple")
java.lang.IllegalArgumentException: No matching clause: class
java.lang.String (
NO_SOURCE_FILE:0)
--
An error gets thrown even though I provided a matching clause. I
believe it has something to do with the 'case' macro but I am
probably wrong.
I rewrote the function as so:
--
(defn panda-2 [x]
    (let [xType (type x)]
        (cond
            (= java.lang.String xType) "HELLZ YEAH IT'S A STRING!"
            (= java.lang.Integer xType) "it's an integer...")))
--
It works as expected but I would really like to be able to use the
'case' macro instead because it would make my code prettier and I like
pretty code.
Thank you for your time!



-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to