Hi, I’m learning Elixir by doing Exercism courses. I’m on the LogLevel exercise that has to do with Atoms and Cond, I’ve been pulling my hair out over the failing test for a cond evaluation.
Here’s the issue, in the code snippet below, if the log level is 0 and the legacy? is false, it should return an atom of :trace, otherwise it should return an atom of :unknown. The tests for the two lines that have an and operator in the cond evaluation are failing and i don’t know why. I pasted the code in my local editor and there were no errors, I’m confused. Is there something I’m doing wrong?
defmodule LogLevel do
def to_label(level, legacy?) do
# Please implement the to_label/2 function
cond do
(level === 0 and legacy? == false) -> :trace
level === 1 -> :debug
level === 2 -> :info
level === 3 -> :warning
level === 4 -> :error
(level === 5 and legacy? == false) -> :fatal
true -> :unkown
end
end
def alert_recipient(level, legacy?) do
# Please implement the alert_recipient/2 function
end
end
Yep, that’s the reason. In Rust / OCaml / Haskell you just define one sum type (enum in Rust) and if you have a typo the program doesn’t compile.
Still though, for Elixir you can just start doing Ctrl-F (namely find) occurrences of what you think should be in the code and check for yourself. Or you can use grep with counting matches and you can manually verify that. There are many ways to reduce the blind spots of the dynamic languages.
True, but let’s not forget that somebody has to make this proper test. This is a fact in this exercise but it’s not always a fact in the commercial projects.
I definitely have to get better at reading Elixir error messages, thanks for pointing this out. I honestly don’t know how my brain could not parse the typo for hours!