Welcome! Please see the About page for a little more info on how this works.

0 votes
in Compiler by

Calling class will return an instance of the object’s class, which will print as the class name but is hard to actually use (such as in case branches). It would be helpful to have a helper function that returns the symbol representation of the object’s class.

by
I don't think we're going to add this, seems pretty trivial to make yourself.
by
heh I suspected as much. Make a top level answer and I’ll mark it as complete, if you care about such things.
by
we can leave it open and see if this accumulates anything over time, that's just my first impression

2 Answers

+1 vote
by

The easiest way to do this is:

(case (.getName (class c))
  "java.lang.String" "We got a string!"
  "[[I"              "An array of arrays of int.")

I suggest just using strings rather than trying to make them symbols because Java class names can get funky, with leading square brackets if it is an array, and embedded dollar signs if it is a nested class.

Also keep in mind that just a name is not enough to uniquely identify a class; you may have classes with the same name loaded by different class loaders which are not mutually compatible with each other. For most situations where you are thinking about this kind of a case expression, however, this probably won’t be an issue.

0 votes
by

(symbol (.getName cls))

You still won't be able to use it with case since it expects literals and not bindings/forms. But you can use (condp = ...), although at that point it might make more sense to use it with instance?.

by
I was thinking of `(case (class obj) java.some.Class :a java.other.Class :b)`.
...