Re: class and case

2017-05-13 Thread Timothy Baldridge
Using a protocol is probably the optimal choice, since it also opens up the
dispatch allowing for extension without modifying exiting code. Downstream
users of your library can add extensions to your code without having to
create a patch.



On Sat, May 13, 2017 at 7:43 AM, Alex Miller  wrote:

> Or case on (.getName the-class).
>
> --
> 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.
>



-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
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.


Re: class and case

2017-05-13 Thread Alex Miller
Or case on (.getName the-class).

-- 
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.


Re: class and case

2017-05-12 Thread John Mastro
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.


class and case

2017-05-12 Thread Kevin Kleinfelter
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?

Tnx.

-- 
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.