1

com.rabbitmq.client.ConnectionFactory.newConnection(Address[])从 Clojure 1.4.0 调用,如下所示:

(defn connect [#^ConnectionFactory factory, addrs]
  (.newConnection factory (into-array Address addrs)))

该调用有效,但会引发反射警告:

call to newConnection can't be resolved.

回顾ConnectionFactory类,显然有一种形式只接受一个类型的参数com.rabbitmq.client.Address[],这正是(into-array Address ())返回的内容:

com.mefesto.wabbitmq=> (pprint (filter #(.. (.toString %)
                                            (contains "newConnection"))
                               (seq (.getDeclaredMethods
                                     ConnectionFactory))))
(#<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      com.rabbitmq.client.Address[])
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      java.util.concurrent.ExecutorService,
      com.rabbitmq.client.Address[])
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection()
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      java.util.concurrent.ExecutorService)
    throws java.io.IOException>)

com.mefesto.wabbitmq=> (into-array Address ())
#<Address[] [Lcom.rabbitmq.client.Address;@953235f>

我需要做些什么来避免在这里反射?

4

1 回答 1

1

不幸的是,高阶函数总是使用 Object 作为它们的参数类型,并且编译器还没有在调用 HOF 中遵循类型提示。这基本上是因为它编译了匿名函数

#(.. (.toString %) (contains "newConnection"))

在它知道如何使用它之前。这很可能很快就会改善。

如果您可以解决以下问题,您也许可以解决此问题filter

于 2012-04-27T21:00:45.667 回答