为什么Clojure说“没有匹配方法”为非法论证? [英] Why does Clojure say "no matching method" for an illegal argument?

查看:158
本文介绍了为什么Clojure说“没有匹配方法”为非法论证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确使用字符/ isWhitespace的用法包括:

Correct usages of Character/isWhitespace include:

(Character/isWhitespace \a) => false
(Character/isWhitespace \ ) => true

但是,我的第一次尝试是这样,我发现错误让人困惑。

However, my first attempt was this, and I find the error confusing.

(Character/isWhitespace "")
  =>  IllegalArgumentException No matching method found: isWhitespace
  => clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)

IllegalArgument 部分有意义,但为什么说找不到匹配方法?显然该功能确实存在。

The IllegalArgument part makes sense, but why does it say "no matching method found"? Clearly the function does exist.

我问这个问题的原因是

当我输入(Character / isWhitespace \a),我认为我说:我知道有一个字符命名空间,在其中有一个函数 isWhitespace ,我想调用该函数并传入 \a

When I type (Character/isWhitespace \a), what I think I'm saying is: "I know that there's a Character namespace, and within that there's a function called isWhitespace, and I want to call that function and pass in \a".

在这个心理模型上,我上面的结果很混乱,因为看起来Clojure说,每当你给我一个参数类型,这个函数不接受,我会假装函数doesn' t存在。例如,你不允许混合砖,所以如果你尝试,我会给你一个BlenderDoesntExist错误。

On this mental model, my results above are confusing because it seems like Clojure is saying, "whenever you give me an argument type that this function doesn't accept, I'm going to pretend the function doesn't exist." Eg, "you're not allowed to blend bricks, so if you try, I'm going to give you a BlenderDoesntExist error." Which is weird.

一些答案​​似乎暗示了

Some answers seem to imply that


  • code> Character / isWhitespace 只是Clojure用来查找函数的部分,另一部分是参数的类型。 (我已经做了一些更多的搜索:这是一个多方法吗?)

  • 该方法是在Java类上查找的

  • The name Character/isWhitespace is only part of what Clojure uses to look up the function, and the other part is the type of the argument. (I've done some more searching: is this maybe a multimethod?)
  • The method is being looked up on a Java class?

一个伟大的答案会为我澄清这个过程。

A great answer would clarify this process for me.

推荐答案

Character / isWhitespace 是一个Java方法( java.lang.Character 类的静态方法)。有关调用Java方法的语法示例,请参见 Java interop

Character/isWhitespace is a Java method (a static method of the java.lang.Character class). See Java interop for examples of syntax that call Java methods.

然而,常规的Clojure函数仅由它们的名称定义,Java方法由它们的签名,包括他们的姓名及其参数的数量和类型。

Whereas regular Clojure functions are defined by their name alone, Java methods are defined by their signature which consist of their name and the number and types of their parameters.

isWhitespace rel =nofollow>字符 isWhitespace(char ch) isWhitespace(int codepoint) 。因此,有一个没有匹配的方法调用 isWhitespace 与一个字符串。

The only variants of the isWhitespace method defined in the Character class are isWhitespace(char ch) and isWhitespace(int codepoint). So there's "no matching method" for calling isWhitespace with a string.

这篇关于为什么Clojure说“没有匹配方法”为非法论证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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