slouchpie

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:

  1. What is the best way to achieve the desired outcome? Should I just use two separate case expressions?
  2. Imagine if the first clause adds something to the database with Repo.insert. Does Ecto know it is inside a with expression and wait for all clauses to succeed before committing the transaction? :thinking: 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

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

wolf4earth

For complex multi-step processes where rollback steps are necessary, you might want to look into the “saga” pattern.

There is Sage which is an implementation of the saga pattern in Elixir.

slouchpie

slouchpie

This forum is so darn good. If it had dark mode it would be the best website on the internet.

slouchpie

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.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement