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"))
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
Last Post!
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









