为什么默认情况下,when-let和if-let不支持多个绑定? [英] Why don't when-let and if-let support multiple bindings by default?

查看:161
本文介绍了为什么默认情况下,when-let和if-let不支持多个绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 when-let if-let 默认支持多个绑定?

Why don't when-let and if-let support multiple bindings by default?

所以:

(when-let [a ...
           b ...]
  (+ a b))

p>

...instead of:

(when-let [a ...
  (when-let [b ...
    (+ a b)))



我知道我可以编写自己的宏或使用monad这里: http://inclojurewetrust.blogspot.com/2010/12/when-let-maybe。 html )。

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).

推荐答案

因为 if-let ,至少)对else情况做什么并不明显。

Because (for if-let, at least) it's not obvious what to do with the "else" cases.

至少,由更好的方式嵌套if-let在clojure 我开始写一个宏这样做。给定

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)

会产生

(if-let [a ...]
  (if-let [b ...]
    action
    ?))

,我不清楚如何继续(有两个地方为else)。

and it wasn't clear to me how to continue (there are two places for "else").

你可以说对于任何失败应该有一个替代方法,或者对于当没有<-c $ c> when-let 测试突变状态,然后事情仍然会变得麻烦。

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.

另一种说同样的方法:你假设 if-let 应嵌套 let 。一个更好的模型可能是 cond ,这不是一个嵌套if,但更多的替代if,因此不适合良好的范围...或,还有另一种说法:如果不处理这种情况任何更好。

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屋!

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