Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical tips about with (in Elixir)

Wu Qing
October 03, 2018

Practical tips about with (in Elixir)

Presented at October 2018 meetup of Elixir Sydney
https://www.meetup.com/sydney-ex/events/kznqrpyxnbfb/

Wu Qing

October 03, 2018
Tweet

More Decks by Wu Qing

Other Decks in Programming

Transcript

  1. conditionals case function1() do {:ok, result} -> ... {:ok, "all

    good"} {:error, reason} -> ... {:error, reason} end
  2. nested conditionals case function1() do {:ok, result1} -> case function2()

    do {:ok, result2} -> ... {:ok, "all good"} {:error, reason2} -> ... {:error, reason2} end {:error, reason1} -> ... {:error, reason1} end
  3. use with ☺ with {:ok, result} <- function1() do ...

    end # or with {:ok, result} <- function1() do ... else {:error, reason} -> ... {:error, reason} end
  4. ! with {:ok, result1} <- function1(), {:ok, result2} <- function2()

    do ... else {:error, reason} -> ... {:error, reason} end
  5. else in with ! with {:ok, result} <- function1() do

    ... else {:error, reason} -> {:error, reason} end
  6. Be careful ❗ with {:ok, result} <- function1() do ...

    else {:error, :specific_error} -> ... end
  7. Have a match all clause ! with {:ok, result} <-

    function1() do ... else {:error, :specific_error} -> ... {:error, "function1 failed because ..."} {:error, reason} -> {:error, reason} end