【问题标题】:Clojure warn-on-reflection and type hintsClojure 反射警告和类型提示
【发布时间】:2011-06-06 03:28:42
【问题描述】:

在以下代码中,我收到了关于反射的警告:

(ns com.example
  (:import
    [org.apache.commons.cli CommandLine Option Options PosixParser]))

(def *help-option* "help")
(def *host-option* "db-host")

(def *options*
  (doto (Options.)
    (.addOption "?" *help-option* false "Show this usage information")
    (.addOption "h" *host-option* true "Name of the database host")))

(let
  [^CommandLine command-line (.. (PosixParser.)
                               (parse *options* (into-array String args)))

   db-host (.getOptionValue command-line "h")]  ; WARNING HERE ON .getOptionValue
  ; Do stuff with db-host
)

我在command-line 上有一个类型提示。为什么会出现警告?

我在 OS X 10.6.6(Apple VM)上使用 Clojure 1.2。

我假设我确实不会(.addOption ...) 上收到警告,因为编译器知道(Options.)org.apache.commons.cli.Options)

【问题讨论】:

    标签: clojure type-hinting


    【解决方案1】:

    我发现了问题。由于*host-option* var,Clojure 无法确定要调用的方法。如果我将 var def 更改为 (def ^String *host-option* "db-host") 它会消除警告。

    我更新了我的问题以显示我的代码中实际包含的def

    【讨论】:

      【解决方案2】:

      在 clojure 1.2 中,每个函数的返回类型都是对象。您可能已经想到了这一点,但警告可能来自命令行的 .getOptionValue 方法返回的值

      【讨论】:

        猜你喜欢
        • 2017-04-22
        • 2019-05-27
        • 2019-11-25
        • 1970-01-01
        • 2019-10-18
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多