我最近认为我在clojure.spec
中遇到了一个bug,因为它在将类似于(fn [x] x)
或的lambda表达式放入->
线程宏而未先将其包裹在额外的括号中如((fn [x] x))
时抛出异常。这是我在Clojure中使用过程中最常见的错误之一,我也从其他人那里听说这是一个关于线程宏的常见误解。我在这个错误案例中的假设是lambda是一个语言中的基本“事物”或值。但事实上,Clojure仅仅是数据,大多是宏,每个宏都有其自己的数据处理方式。
->
线程宏将lambda视为列表而不是基本“函数值”。当进行(-> :hmm (fn [x] x))
时,它会展开为(fn :hmm [x] x)
,这显然不是预期的。然而,它有一个特别的方便情况,即对于非列表事物,如符号、关键词等,假设它是一种可以以一个参数为行为的东西,并在线程之前将其包裹在括号中。这个特殊情况增加了价值,因为它解释了用户的意图,避免了强迫他们在不需要时添加括号。
我断言基本的lambda形式(fn)
和#()
应该并以类似符号的方式处理,通过解释用户的意图。这样做的好处是使线程宏更简单、更容易使用,并消除影响众多用户的必要异常,并将语言推进到将lambda形式视为核心“值”的方向。最后一点是哲学性的,但对我来说非常重要,因为它使得语言“只做正确的事”,这是新手或非专家用户在完成任务时的预期。
将非列表表单包裹起来的现有特殊案例已经证明了提供一些对用户意图解释的价值,但现在已经存在这种特殊案例与对于语言中最基础的“值”之一的函数的两个最常见形式之间的不一致。
你是否曾被这种“错误”困扰过?
如果您想与现有的->
宏并排尝试它,这里有一个名为t->
的版本。
(defmacro t->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
我已经为 ->
和 ->>
准备了补丁,以处理这个用例,并且我认为它不会产生破坏性的影响,即保持现有行为并添加新功能。它通过了所有核心Clojure测试,但在我自己的代码之外,我还没有对其进行过测试。在将其视为核心的潜在更改之前,这显然是必要的。
From 686831062a574486413022af31e8c7a07b78cd24 Mon Sep 17 00:00:00 2001
From: Thomas Spellman <[email protected]>
Date: Mon, 13 Jan 2020 20:39:45 -0800
Subject: [PATCH] thread macros
---
src/clj/clojure/core.clj | 12 ++++++------
test/clojure/test_clojure/macros.clj | 12 ++++++++++++
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 8e98e072..fe43289b 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1670,42 +1670,42 @@
(. (. System (getProperties)) (get \"os.name\"))
but is easier to write, read, and understand."
{:added "1.0"}
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more)))
(defmacro ->
- "Threads the expr through the forms. Inserts x as the
- second item in the first form, making a list of it if it is not a
+ "Threads the expr through the forms. Inserts x as the second item
+ in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
- threaded (if (seq? form)
+ threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(defmacro ->>
- "Threads the expr through the forms. Inserts x as the
- last item in the first form, making a list of it if it is not a
+ "Threads the expr through the forms. Inserts x as the last item
+ in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc."
{:added "1.1"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
- threaded (if (seq? form)
+ threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(def map)
(defn ^:private check-valid-options
diff --git a/test/clojure/test_clojure/macros.clj b/test/clojure/test_clojure/macros.clj
index ce17bb38..9fb1fa9e 100644
--- a/test/clojure/test_clojure/macros.clj
+++ b/test/clojure/test_clojure/macros.clj
@@ -106,8 +106,20 @@
(is (nil? (loop []
(as-> 0 x
(when-not (zero? x)
(recur))))))
(is (nil? (loop [x nil] (some-> x recur))))
(is (nil? (loop [x nil] (some->> x recur))))
(is (= 0 (loop [x 0] (cond-> x false recur))))
(is (= 0 (loop [x 0] (cond->> x false recur)))))
+
+(deftest ->lambda-test
+ (is (= 'a (-> 'a ((fn [x] x)))))
+ (is (= 'a (-> 'a (fn [x] x))))
+ (is (= 'a (-> 'a #(identity %))))
+ (is (= 'a (-> 'a (#(identity %))))))
+
+(deftest ->>lambda-test
+ (is (= 'a (->> 'a ((fn [x] x)))))
+ (is (= 'a (->> 'a (fn [x] x))))
+ (is (= 'a (->> 'a #(identity %))))
+ (is (= 'a (->> 'a (#(identity %))))))
--
2.21.0 (Apple Git-122.2)
记录: https://clojure.atlassian.net/browse/CLJ-2553