简体   繁体   中英

How can I cast a Java class in Clojure?

I would like to cast a clojure Java object (assigned with let*) to another Java class type. Is this possible and if so then how can I do this?

Update: Since I posted this question I have realised that I do not need to cast in Clojure as it has no concept of an interface, and is more like Ruby duck typing. I only need to cast if I need to know that an object is definitely of a certain type, in which case I get a ClassCastException

There is a cast function to do that in clojure.core :

user> (doc cast)
-------------------------
clojure.core/cast
([c x])
  Throws a ClassCastException if x is not a c, else returns x.

By the way, you shouldn't use let* directly -- it's just an implementation detail behind let (which is what should be used in user code).

Note that the cast function is really just a specific type of assertion. There's no need for actual casting in clojure. If you're trying to avoid reflection, then simply type-hint:

user=> (set! *warn-on-reflection* true)
true
user=> (.toCharArray "foo")  ; no reflection needed
#<char[] [C@a21d23b>
user=> (defn bar [x]         ; reflection used
         (.toCharArray x))
Reflection warning, NO_SOURCE_PATH:17 - reference to field toCharArray can't be resolved.
#'user/bar
user=> (bar "foo")           ; but it still works, no casts needed!
#<char[] [C@34e93999>
user=> (defn bar [^String x] ; avoid the reflection with type-hint
         (.toCharArray x)) 
#'user/bar
user=> (bar "foo")
#<char[] [C@3b35b1f3>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM