Skip to content

Instantly share code, notes, and snippets.

@leandrocp
Last active April 10, 2016 05:48
Show Gist options
  • Save leandrocp/9ba71457bb902c4b7ed2 to your computer and use it in GitHub Desktop.
Save leandrocp/9ba71457bb902c4b7ed2 to your computer and use it in GitHub Desktop.
Elixir With special form
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}
# 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)
@divmgl
Copy link

divmgl commented Apr 10, 2016

When you have the with keyword, how do you specify an error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment