The “with” Statement

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

4 Comments

  1. Maksym Kosenko on May 8, 2022 at 7:55 am

    Should it be Users.get(1) instead of Users.one(1) and the same for Customers in the paragraph “Telling the Difference” code example ?

    • Mark Ericksen on May 9, 2022 at 2:54 am

      Hey Maksym,

      To be consistent with the practice project, it should be .one. So I updated the instructions to use .one also. As it was written, you were right, it should have been .get. Hopefully this helps clear up the confusion! Thanks for reporting the issue.

  2. Maksym Kosenko on May 8, 2022 at 9:10 am

    I have a question. Where on “with… do .. end” it’s better put IO.inspect in to investigate what the function call returns?!

    • Mark Ericksen on May 9, 2022 at 2:58 am

      Great question!

      Just remember you can stick IO.inspect anywhere with a pipe. In this example, when I’m playing around and trying to figure out what the output of the steps are, I can write an IO.inspect on each line of the with. It’s not as clean as it can be in a pipeline, but it works!

      with {:ok, width} <- Map.fetch(data, :width) |> IO.inspect(label: "WIDTH"),
           {:ok, height} <- Map.fetch(data, :height) |> IO.inspect(label: "HEIGHT") do
        {:ok, width * height}
      end
      

      Doing it like this still outputs the result before the pattern match on the left, so you’ll see any unexpected output that way.

Leave a Comment

You must be logged in to post a comment.