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 https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1. 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.

egze

egze

+1 for Ecto.Multi. Seems much nicer for this use-case.

Lately I use this approach for most of my code and it works well for me: https://luizdamim.com/blog/reorganizing-your-phoenix-contexts-as-use-cases/

bluejay

bluejay

Go for it :+1:

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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 Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement