When to use “if” and “when” in Clojure?

前端 未结 2 1519
情深已故
情深已故 2020-12-15 04:30

When is one better than the other? Is one faster than the other or does the only difference is the return of false or nil?

2条回答
  •  离开以前
    2020-12-15 04:47

    Use if when you have two different expressions: for true clause and for false clause.

    when and when-not are useful in two cases:

    • when you want to perform one or several (implicit do helps here) non-pure operations conditionally;
    • when you want to evaluate something when some predicate evaluates to true (or false in case of when-not), and return nil in opposite case.

    only difference is the return of false or nil

    There is no major difference between false and nil, as the both evaluate to false in logical context.

提交回复
热议问题