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

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

问题描述

Character/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.

我问这个问题的原因是我是 Clojure 的新手,并且认为我从根本上误解了一些东西.

The reason I'm asking this question is that I'm new to Clojure, and think I'm fundamentally misunderstanding something.

当我输入 (Character/isWhitespace a) 时,我认为我想说的是:我知道有一个 Character 命名空间,其中有一个名为 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 似乎在说,每当你给我一个这个函数不接受的参数类型时,我就会假装这个函数不存在."例如,你不允许混合砖块,所以如果你尝试,我会给你一个 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.

有些答案似乎暗示

  • 名称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 互操作.

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.

Character 类是 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天全站免登陆