Cond evaluation failing on LogLevel exercise

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

Not sure about the rest because I don’t remember the exercise but you have a typo here.

:unkown:unknown

2 Likes

Oh my God! I feel so dumb! Thank you so much! That solved it. I’d been going at it all day, who knew all I needed was a second pair of eyes. Thanks!

The woes of dynamic languages. :man_shrugging: Elixir is fantastic but Rust would have yelled at you on the first compilation.

Glad that you are unblocked!

1 Like

Now I see why programmers will live and die for statically typed languages :smile:

1 Like

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. :slight_smile:

1 Like

Static typing still gives you error messages you have to correctly understand to find the error; for instance, the failing test for this exercise:

will fail on the assert LogLevel.to_label(0, true) == :unknown line with a message:

  1) test LogLevel.to_label/1 level 0 has label trace only in a non-legacy app (LogLevelTest)
     test/log_level_test.exs:6
     Assertion with == failed
     code:  assert LogLevel.to_label(0, true) == :unknown
     left:  :unkown
     right: :unknown
     stacktrace:
       test/log_level_test.exs:8: (test)
4 Likes

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.

2 Likes

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!

Thank you! I’ll take note and add that to my debugging toolkit :pray: