Kevin Kleinfelter <kleinfelter.gro...@gmail.com> wrote:
> Can someone help me get un-stuck with a problem of using class in a case?
>
> This returns true, as I expect:
> (= (type "x") java.lang.String)
>
> This returns an error, and I'd hoped it would print STRING:
> (case (type "x")
>  class java.lang.String (println "STRING")
>  class java.lang.Long (println "NUM"))
>
> Because the error said "No matching clause: class java.lang.String", I
> thought maybe this would be better, but if gave the same error:
> (case (type "x")
>  class java.lang.String (println "STRING")
>  class java.lang.Long (println "NUM"))
>
> Is in not possible to match on class in a case?

Correct, because the test expressions have to be compile-time constants.
Also, "class java.lang.String" isn't valid Clojure syntax for an
expression. Just "java.lang.String" evaluates to the class with that
name, but the test expressions aren't evaluated.

I think the most common approach for this scenario is to use condp:

    (condp instance? "x"
     java.lang.String "STRING"
     java.lang.Long "LONG")

Hope that helps

        John

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to