slouchpie
Advice for working around my _only_ problem with "with" expressions - accessing success results
Consider this code:
with {:a, first_result} <- {:a, List.first(["apple"])},
{:b, b} <- {:b, %{} |> to_string} do
{:ok, "happy path"}
else
{:a, _} -> {:error, "this will never be a problem"}
{:b, error} -> {:error, "Why can I not print #{first_result} here?"}
end
I have learned by trial-and-error that this code will not return
{:error, "Why can I not print apple here?"}
as I originally expected from this kind of syntax. Instead, the compiler tells me that first_result is not defined in the else block. It makes sense, because the “clause chain” returns the error result of the failing clause.
So I have 2 questions:
- What is the best way to achieve the desired outcome? Should I just use two separate
caseexpressions? - Imagine if the first clause adds something to the database with
Repo.insert. Does Ecto know it is inside awithexpression and wait for all clauses to succeed before committing the transaction?
That seems unlikely. So then, why does this expression not allow us to access any successful results?
It seems like I should be very careful when using with since, if I understand correctly (I probably don’t), I can have “partial success” without being able to see the successful results.
Any advice and insight is welcome! I hope the question is clear.
P.S. I have already read Kernel.SpecialForms — Elixir v1.20.2. It just says “the chain is aborted”.
P.P.S. get your SO points here: https://stackoverflow.com/questions/63394081/advice-for-working-around-my-only-problem-with-with-expressions-accessing
Marked As Solved
bluejay
This works:
with {:a, first_result} <- {:a, List.first(["apple"])},
{:b, first_result, :something} <- {:b, first_result, :other_thing} do
{:ok, "happy path"}
else
{:a, _} -> {:error, "this will never be a problem"}
{:b, first_result, _} -> {:error, "Now I can print #{first_result} here"}
end
# => {:error, "Now I can print apple here"}
Also Liked
wolf4earth
slouchpie
This forum is so darn good. If it had dark mode it would be the best website on the internet.
slouchpie
I just want to say thanks again for showing Sage to me. I have been using it for a few months and it makes my life so much easier.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









