silverdr
I am looking for something like in this simplified example:
with\
%{key: true} <- result = a_function_returning_map()
do
:success
else
_ -> IO.inspect(result)
end
The with construct seems like a good fit for the stream of various actions that may fail and return different results. I can recognise which of them failed based on a specific “fingerprint” of the failure but would like to have full result too. Any suggestions? Or back to nested cases/ifs and Co.?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I have spent the past 2 weeks experimenting with the idea of adapting the tiling model used by window managers like TUIOS or BSPWM. The e...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ruslandoga
Good and Bad Elixir might give you some ideas.
silverdr
Well, I read it (thank you) and basically it says “Back to deeply nested
cases/ifs and Co.”, although not because there’s no way to pass result value(s) down toelse. Rather because the author believes it to be at all “bad” to match specific errors in theelseblockgregvaughn
You’re throwing away the parameter passed to the
elseblock. It will receive the result of the expression that failed.silverdr
That’s because I simplified the example down to the very core
Normally I’d match things there. The question was about passing additional information/variable, which (leaving aside whether this is “good” or “bad”) I am unable to do. But in retrospect it may not make much sense, indeed 
gregvaughn
It can be done with wrapping tuples, but it’s ugly. You have to do things like:
If you find yourself in that situation, then it’s probably worth stepping back to look at the bigger picture for other design approaches.
But the basic fact is that those bindings available in the expressions and do block are no longer in scope during the
elsestefanchrobot
Not quite. What about the suggestion to introduce a unified error struct? We went with that approach and it works pretty nicely. The error struct doesn’t have to be global to the whole app - in our case we have an error struct per app domain. This requires some plumbing code but in practice it’s not that much of work.
silverdr
I see.Thank you. Didn’t think of that. Maybe because
indeed
Full ACK. Thanks once more.
Quite literally in fact
This comes later in the article section and it is very much OK. In fact similar to what I am used to do in other languages when justified. Here, I may not yet be as well-versed in Elixir as I am in numerous other languages but when I am confronted with a well confined piece of not-really-reusable business logic I rather take a dozen lines of the author’s perfectly readable “Bad Elixir” than the larger, more complex and less readable “Good Elixir”
I think the
elsewas added to the language at some point for a reason. And the fact that it actually enforces matching the failed expression is for a reason too. Obviously this shouldn’t be abused to create monstrous blocks of rather brittle code - that would be a completely different story.stefanchrobot
The
elseclause was there from the beginnings ofwith. Looking through Elixir’s codebase, I haven’t really found instances of using more than one patterns inelse. So it’s either falling through without theelseclause:or returning a default regardless of an error:
or using the approach suggested by the author, but without a dedicated error struct:
I’m not familiar with the codebase, but the last one looks like a use of
withto build some flow (something that you’d use in application code) rather than just mechanics of avoiding nestedcase(something you’d use more in library code)josevalim
Correct. I generally find having more than one clause in
elsean anti-pattern. We either use it as a “catch-all”, otherwise I prefer to have each function inwithalready return the proper types, as shown in your last example.silverdr
This source says: " As of Elixir 1.3,
with/1statements supportelse", which I interpret that it was added in 1.3. Correct me if I am wrong or the source is wrong.I surely would prefer that too. But the reason why I wanted to use
withwas actually that I didn’t have that kind of comfort and it looked like an easy/elegant way of integrating a flow of different, non homogeneous steps without building a large, nestedif/casebow.