pillaiindu
Why in the cond we must use true as the final condition? Why can't we use _?
For example the following,
iex(16)> cond do
...(16)> 2 + 2 == 5 ->
...(16)> "Not true"
...(16)> 2 * 2 == 3 ->
...(16)> "nor this"
...(16)> _ ->
...(16)> "will match"
...(16)> end
** (CompileError) iex:21: invalid use of _ inside "cond". If you want the last clause to always
match, you probably meant to use: true ->
Most Liked
Ted
I think this is worth reiterating:
For example, Clojure’s cond works in similar fashion, although the convention there is to use :else instead of true, e.g.,:
(let [x 11]
(cond
(< x 2) "x is less than 2"
(< x 10) "x is less than 10"
:else "x is greater than or equal to 10"))
https://clojure.org/guides/learn/flow#_cond
And :else also works in Elixir:
iex(6)> x = 11
11
iex(7)> cond do
...(7)> x < 2 -> "x is less than 2"
...(7)> x < 10 -> "x is less than 10"
...(7)> :else -> "x is greater than or equal to 10"
...(7)> end
"x is greater than or equal to 10"
true, :else, "default", etc. are all truthy and therefore serve the purpose.
Personally, I kinda like :else -> or :default -> but the Elixir community appears to have settled on true -> so I’ll use that.
peerreynders
I understand that you are only referring to a mental model - but as you know in Elixir a literal if is actually implemented a case expression, while cond is it’s own special form (so if taken literally this could be confusing).
Essentially the first expression that evaluates to a truthy value “wins” and consequently the expression to the right of that -> is evaluated and becomes the value of the cond expression. In a sense the cond expression is enclosing a sequence of condition -> expression constructs.
Why can’t we use _?
cond by design
Raises an error if all conditions evaluate to
nilorfalse.
so
it may be necessary to add a final always-truthy condition (anything non-
falseand non-nil)
_ would imply that any value is OK.
In the end it makes the design of cond simpler
(in the true sense of the word - if ... else if ... else .. end is more familiar).
true -> expression
is no different from the other
condition -> expression
before it - i.e. there is no need for a special else part inside cond.
NobbZ
Beacause _ is only valid in a match and cond is only expanding to a bunch of nested ifs.
Qqwy
If you want to get really fancy with it and make sure that nobody else will understand your code any more, you could even use :_
. don’t do this please
DevotionGeo
In other words you’re not matching something, but are checking which statement is true.







