- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们几乎可以立即在运行时重新加载 Clojure 中的任何函数和/或变量。我们甚至可以更改方法签名。对于 Scala 或 Java,我们能做的最多就是使用速度慢、商业化和受限的 JRebel。让 Clojure 具有如此交互性的区别是什么?
在 Slack 中阅读此内容时,我发现了以下评论,但我想了解更多。还感谢进一步澄清问题的论文/文章的链接(尽管不是必需的)。
It’s mostly because the language is set up to be reloadable. Clojure has a var indirection for every function or top level variable definition which you can mutate, so you can redefine just one function while keeping the rest of your environment the same and carry on
following up on that - there's indirection when the function name is in the code, but for a long running function that took another function as an argument (eg. you passed a handler function to an http server process startup) you can get the benefits of var indirection by hand - by passing #'handler instead of handler but otherwise you don't get the reloading (without restarting the process that took that arg)
kind of
direct linking replaces var calls being compiled with direct calls (edited) the var path however still exists and NEW code can still invoke via the vars
最佳答案
您所问的关键在于 Clojure 如何识别函数并在运行时运行它们。首先,Clojure 函数定义为 vars
,这是它们的 JVM 根类的 Clojure 名称,Var
.
Clojure 的运行时维护单个 ConcurrentHashMap
调用Namespaces
.这张 map 有Symbol
键(命名空间名称)和 Namespace
值(value)观。每个Namespace
反过来有 AtomicReference
'd Clojure map
(称为“映射”)是动态类型的,但本质上具有 Clojure Symbol
键(局部变量名)和Var
值(value)观。
当您调用 Clojure 函数时,它首先会在 Namespaces
中查找您引用的命名空间。然后在该命名空间的映射中查找特定变量。这使得热加载代码变得微不足道 - 您需要做的就是设置一个新的 <Symbol, Var>
对给定命名空间的映射。
更深一层,Clojure 还保持对“框架”(即线程或可能临时重新定义局部范围内的变量的附加绑定(bind))的认识。这些有自己的ThreadLocal
storage 和在其中之一中找到的变量将被使用,而不是当前存储在命名空间的映射中的变量。
Clojure 的方法在这里是可行的,因为它不尝试将函数存储为 JVM 函数,而是作为 Java 对象本身保存在可以快速访问的映射中。
Clojure 通过检查它们是否满足函数接口(interface) (IFn
) 知道这些对象实际上是可调用的。一个对象满足 IFn
通过拥有 Invoke
方法。这用于许多非常聪明的目的,并解释了为什么 Clojure 的许多核心数据结构(映射、向量、关键字等)也都可以作为函数调用。
关于clojure - 为什么 Clojure 比其他 JVM 语言更可热插拔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49943143/
我是一名优秀的程序员,十分优秀!