Skip to content

Instantly share code, notes, and snippets.

@expede
Last active September 3, 2016 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expede/11fad9ee2e56afcd2129b2acaecd4c27 to your computer and use it in GitHub Desktop.
Save expede/11fad9ee2e56afcd2129b2acaecd4c27 to your computer and use it in GitHub Desktop.
# =====================
# Explicit branch names
# =====================
branch OtherFile.read("./existing_file.txt"),
value_do: String.length,
exception_do: fn %{message: msg} -> msg end.()
# => 1000
branch OtherFile.read("./missing.file"),
value_do: String.length,
exception_do: fn %{message: msg} -> msg end.()
#=> "error message"
# ==========================
# Convenient if/else variant
# ==========================
if_exception OtherFile.read("./existing.file"), do: fn %{message: msg} -> msg end.(), else: String.length
#=> 1000
OtherFile.read("./missing.file")
|> if_exception do
fn %{message: msg} -> msg end.()
else
String.length
end
#=> "error message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment