如何在Clojure中消除歧义 [英] How to disambiguate in Clojure

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

问题描述

我在不导入/不需要/使用任何其他包的名称空间中具有此功能:

I have this function in a namespace that does not import/require/use any other packages:

(defn crash [msg]
  (throw (Throwable. msg)))

Cursive(IntelliJ IDEA IDE插件)突出显示Throwable并给我消息Cannot disambiguate overloads of Throwable.我收到与ExceptionError相同的消息.

Cursive (the IntelliJ IDEA IDE Plugin) highlights Throwable and gives me the message Cannot disambiguate overloads of Throwable. I get the same message with Exception and Error.

我不明白此消息的来源-我怀疑这些Java类是在Java语言文件之外的其他jar文件中定义的.我有什么可以使此消息消失的?

I don't understand the source of this message - I doubt that these Java classes are defined in any other jar files apart from the Java language ones. Anything I can to make this message go away?

这些在project.clj中:

  :dependencies [[org.clojure/clojure "1.6.0"]
                 [net.mikera/imagez "0.8.0"]
                 [org.clojure/math.numeric-tower "0.0.4"]]

推荐答案

Throwable具有两个1-arg构造函数(

Throwable has two 1-arg constructors (doc): one expecting a String and the other expecting a Throwable.

在运行时Clojure会弄清楚(因为在这种特定情况下,对象不可能既是String又是Throwable),但这需要使用反射.

At runtime Clojure figures it out (since, in this specific case, it's impossible for an object to be both a String and a Throwable) but this requires the use of reflection.

msg中添加类型提示以指定要使用的重载将消除反射的需要,并希望可以使Cursive平静下来.

Adding a type-hint to msg to specify which overload you expect to use would remove the need for reflection and hopefully calms Cursive down.

(defn crash [^String msg]
  (throw (Throwable. msg)))

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

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