什么是“^:静态”做在Clojure? [英] What does "^:static" do in Clojure?

查看:101
本文介绍了什么是“^:静态”做在Clojure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure core.clj 源码中看到了 ^:static 代码,例如在 seq?的定义中:

I've seen the ^:static metadata on quite a few function in the Clojure core.clj source code, e.g. in the definition of seq?:

(def
 ^{:arglists '([x])
   :doc "Return true if x implements ISeq"
   :added "1.0"
   :static true}
 seq? (fn ^:static seq? [x] (instance? clojure.lang.ISeq x)))

这个元数据是什么,以及为什么它在 core.clj ?中经常使用

What precisely does this metadata do, and why it it used so frequently throughout core.clj?

推荐答案

在开发Clojure 1.3 Rich想添加函数的能力,以返回类型而不是Object。

In the development of Clojure 1.3 Rich wanted to add the ability for functions to return types other than Object. This would allow native math operators to be used without having to cram everything into one function.

原始的实现需要支持这个标记的函数, :static 。这个元数据导致编译器为函数生成两个版本,一个返回Object,一个返回特定类型。在编译器确定类型将总是匹配的情况下,将使用更具体的版本。

The original implementation required functions that supported this to be marked :static. this meta data caused the compiler to produce two versions to the function, one that returned Object and one that returned that specific type. in cases where the compiler determined that the types would always match the more specific version would be used.

这之后是全自动的,所以你不需要再添加。

This was later made fully automatic so you don't need to add this anymore.

这篇关于什么是“^:静态”做在Clojure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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