Are classes supposed to work in "case"?

115 views
Skip to first unread message

Aaron Cohen

unread,
Feb 18, 2012, 12:09:07 AM2/18/12
to cloju...@googlegroups.com
=>(case (class 1)
java.lang.Long :long
:other)
:other

Mikera

unread,
Feb 18, 2012, 6:54:15 AM2/18/12
to Clojure Dev
case requires the constants to be compile-time literals.

I think what you are actually doing here is comparing against the
*symbol* java.lang.Long

=> (case 'java.lang.Long
java.lang.Long :long
:other)
:long

Tom Hicks

unread,
Feb 18, 2012, 12:58:36 PM2/18/12
to Clojure Dev
Maybe the #= reader macro will help you?:

> (def x (java.lang.Long 1))
#'user/x

> (class x)
java.lang.Long

> (case (class x)
#=java.lang.Long :long
#=java.lang.Integer :int
:other)
:long

Stuart Sierra

unread,
Feb 18, 2012, 4:15:30 PM2/18/12
to cloju...@googlegroups.com
Fully-qualified class names could be considered compile-time literals. I see an opportunity for a patch to make `case` work here.

-S

Aaron Cohen

unread,
Feb 18, 2012, 7:27:25 PM2/18/12
to cloju...@googlegroups.com

I think class names are pretty much treated as literals throughout
clojure, they're basically clojure's equivalent of "Object.class" from
java. You have to quote them if you want a symbol.

Fully qualified names always work, non-qualified names do not unless
imported, just as I think is expected.

For instance:

=> (class java.lang.Object)
java.lang.Class

=> (class Object)
java.lang.Class

=> (class 'java.lang.Object)
clojure.lang.Symbol

=> (class 'Object)
clojure.lang.Symbol

=> (class java.util.List)
java.lang.Class

=> (class List)
CompilerException java.lang.RuntimeException: Unable to resolve
symbol: List in this context, compiling:(NO_SOURCE_PATH:13)

=> (import java.util.List)
java.util.List

=> (class List)
java.lang.Class

Is it ok if I create an issue for this in JIRA?

Mikera

unread,
Feb 18, 2012, 9:48:37 PM2/18/12
to Clojure Dev
On Feb 19, 5:15 am, Stuart Sierra <the.stuart.sie...@gmail.com> wrote:
> Fully-qualified class names could be considered compile-time literals. I
> see an opportunity for a patch to make `case` work here.
>

I agree this would be a good thing.

For consistency reasons, I think it also needs to work for deftype /
defrecord defined classes as well. Maybe this comes "for free" by
supporting fully qualified class names but just wanted to make sure
they don't get forgotten here!

In addition I think unqualified class names should work in a way that
is consistent with classname usage in the rest of Clojure, i.e. they
don't need to be fully qualified if they have been imported. So you
should be able to do:

(case (class (String. "Foo"))
String :string
:other)
=> :string
Reply all
Reply to author
Forward
0 new messages