为什么 when-let 和 if-let 默认不支持多重绑定? [英] Why don't when-let and if-let support multiple bindings by default?
问题描述
为什么when-let
和if-let
默认不支持多重绑定?
所以:
(when-let [a ...乙...](+ a b))
...而不是:
(when-let [a ...(当-让 [b ...(+ a b)))
我知道我可以编写自己的宏或使用 monad(如此处所述:http://inclojurewetrust.blogspot.com/2010/12/when-let-maybe.html).
因为(至少对于 if-let
而言)处理else"情况并不明显.
至少,受到 更好的嵌套 if-let 方式的启发clojure 我开始写一个宏来做到这一点.给定
(if-let* [a ...乙...]行动其他)
它会产生
(if-let [a ...](如果让 [b ...]行动?))
我不清楚如何继续(else"有两个地方).
你可以说任何失败都应该有一个替代方案,或者 when-let
没有替代方案,但是如果任何测试改变了状态,那么事情仍然会变得一团糟.>
简而言之,它比我预期的要复杂一些,所以我想当前的方法避免了必须调用解决方案.
另一种说法:您假设 if-let
应该像 let
一样嵌套.更好的模型可能是 cond
,它不是嵌套的 if",而是替代的 if",因此不太适合范围......或者,另一种说法它:if
不能更好地处理这种情况.
Why don't when-let
and if-let
support multiple bindings by default?
So:
(when-let [a ...
b ...]
(+ a b))
...instead of:
(when-let [a ...
(when-let [b ...
(+ a b)))
I am aware that I can write my own macro or use a monad (as described here: http://inclojurewetrust.blogspot.com/2010/12/when-let-maybe.html).
Because (for if-let
, at least) it's not obvious what to do with the "else" cases.
At least, motivated by Better way to nest if-let in clojure I started to write a macro that did this. Given
(if-let* [a ...
b ...]
action
other)
it would generate
(if-let [a ...]
(if-let [b ...]
action
?))
and it wasn't clear to me how to continue (there are two places for "else").
You can say that there should be a single alternative for any failure, or none for when-let
, but if any of the tests mutate state then things are still going to get messy.
In short, it's a little more complicated than I expected, and so I guess the current approach avoids having to make a call on what the solution should be.
Another way of saying the same thing: you're assuming if-let
should nest like let
. A better model might be cond
, which isn't a "nested if" but more an "alternative if", and so doesn't fit well with scopes... or, yet another way of saying it: if
doesn't handle this case any better.
这篇关于为什么 when-let 和 if-let 默认不支持多重绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!