如何在Clojure中转换Java类? [英] How can I cast a Java class in Clojure?

查看:54
本文介绍了如何在Clojure中转换Java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将clojure Java对象(分配给let *)转换为另一种Java类类型.这可能吗?如果可以,我该怎么做?

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?

更新:自从我发布了这个问题以来,我已经意识到我不需要强制转换Clojure,因为它没有接口的概念,更像是Ruby Duck类型.我只需要在知道某个对象肯定是某种类型的情况下进行强制转换,在这种情况下,我会收到ClassCastException

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

推荐答案

clojure.core 中有一个 cast 函数可以做到这一点:

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.

顺便说一句,您不应该直接使用 let * -这只是 let 后面的实现细节(应该在用户代码中使用).

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

这篇关于如何在Clojure中转换Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆