Last active
April 10, 2016 05:48
-
-
Save leandrocp/9ba71457bb902c4b7ed2 to your computer and use it in GitHub Desktop.
Elixir With special form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case File.read("my_file.ex") do | |
{:ok, contents} -> | |
case Code.eval_string(contents) do | |
{res, _binding} -> | |
{:ok, res} | |
error -> | |
error | |
error -> error | |
error | |
end | |
with {:ok, contents} <- File.read("my_file.ex"), | |
{res, binding} <- Code.eval_string(contents), | |
do: {:ok, res} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a customer and a promotional coupon | |
case Repo.insert(%Customer{name: "jose valim"}) do | |
{:ok, customer} -> | |
case Repo.insert(%Coupon{customer: customer, perc: 40}) do | |
{:ok, coupon} -> Notify.success(customer) | |
{:error, result} -> Notify.error(customer) | |
end | |
{:error, result} -> | |
Notify.error(result) | |
end | |
# With | |
with {:ok, customer} <- Repo.insert(%Customer{name: "jose valim"}) | |
{:ok, coupon} <- Repo.insert(%Coupon{customer: customer, perc: 40}) | |
do: Notify.success(customer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you have the with keyword, how do you specify an error?