Case Statement

You cannot view this unit as you're not logged in yet. Go to Account to login.

7 Comments

  1. Ali ELBaitam on November 23, 2020 at 6:38 am

    I got stumped when I attempted the first exercise comparing against the value of “age”. I thought the case expression should be the “age” value which I get from the function parameter: def classify_user(%User{age: age})… I didn’t know how it will work:

    case age do
    age when age >=18 -> {…}
    age when age {…}
    _ ->
    end

    I found it weird and different from other languages. I looked back at the examples in the lesson and they all looked unusual. The case expression usually is not a constant in other languages but in the lesson examples it is: case nil do, case true do, case 10 do!

    Eventually, I had to look at the solution and realized that the case expression is the entire User structure not the age value only.

    I understand the solution but I just wanted to share what someone new to Elixir struggles with.

    • Mark Ericksen on November 23, 2020 at 10:57 am

      Thanks for sharing your experience. I’ll give it some more thought about how it could be improved. Feel free to email me directly with any suggestions! 🙂

      • Ali ELBaitam on November 24, 2020 at 3:23 am

        I think I was overthinking what the exercises are asking 🙂 I have gone through many exercism.io and codewars exercises and so far pattern matching using function is what I used and never needed case..do. Case..do looks like a secondary construct in Elixir (pattern matching on functions which looks similar is used more often). One important thing I learned from this lesson though is the case..do as an expression which will be useful as I am progressing with Elixir.

        • Mark Ericksen on November 24, 2020 at 5:28 am

          Yes, the same pattern matching that applies in a function header applies in a case statement. However a case statement is often needed because you need to call a function to get the data you need to analyze.

  2. Robert Scott on September 27, 2022 at 9:55 am

    My solution was the same as Ali’s. It passes the tests and I don’t see anything wrong with it.

    This course and the Pattern Matching course are fantastic by the way and absolutely incredible value. I am really enjoying learning Elixir after a lifetime (I’m 77) of imperative programming and find these courses a great help. Many thanks for all the work you have put in to creating them.

  3. Sergey Makarichev on December 8, 2022 at 11:59 pm

    Hi, Mark!
    Please specify in this lesson – and the other lessons in this course – in which files I have to find those tests.
    Yes, according to the output I may find out that in this lesson I should look for is case_test.exs, but that may be not obvious.
    In Pattern matching course all files are specified in the lessons.

    • Mark Ericksen on December 9, 2022 at 6:18 am

      Hi Sergey,

      They practice exercises list the test file like this…

      # the whole describe block
      mix test test/case_test.exs:18
      
      # individual tests
      mix test test/case_test.exs:20
      

      Where test/case_test.exs is the file, or are you referring to something else?

Leave a Comment

You must be logged in to post a comment.