# \#with

**URL:** https://forum.elixirforum.com/tag/with/943.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [Using \`with\` as the Elixir replacement for \`let\` in Lisp](https://forum.elixirforum.com/t/using-with-as-the-elixir-replacement-for-let-in-lisp/68029)

<div class="topic-metadata">

**Author:** [@simonelnahas](https://forum.elixirforum.com/u/simonelnahas)\
**Replies:** 11\
**Last updated:** [December 12, 2024, 7:48pm UTC](https://forum.elixirforum.com/t/using-with-as-the-elixir-replacement-for-let-in-lisp/68029 "2024-12-12T19:48:56Z")

</div>

Coming from Clojure, Haskell, and Ocaml I’m very happy about using let for variable scoping. I have then started using with for this in Elixir, even though the documentation suggests using it to combine case clauses. w…

---

## [Proposal: Have the formatter put \`with\`'s \`do\` on its own line](https://forum.elixirforum.com/t/proposal-have-the-formatter-put-with-s-do-on-its-own-line/61994)

<div class="topic-metadata">

**Author:** [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Replies:** 49\
**Last updated:** [April 8, 2024, 6:51pm UTC](https://forum.elixirforum.com/t/proposal-have-the-formatter-put-with-s-do-on-its-own-line/61994 "2024-04-08T18:51:10Z")

</div>

So after complaining about this for the third or fourth time on this forum, I figured I should make a proposal. TL;DR with can be hard to read sometimes as it can be hard to pick out where the clauses end and the body b…

---

## [Dbg() in a \`with\` sequence?](https://forum.elixirforum.com/t/dbg-in-a-with-sequence/60594)

<div class="topic-metadata">

**Author:** [@dogweather](https://forum.elixirforum.com/u/dogweather)\
**Replies:** 6\
**Last updated:** [December 27, 2023, 12:37am UTC](https://forum.elixirforum.com/t/dbg-in-a-with-sequence/60594 "2023-12-27T00:37:18Z")

</div>

dbg() is one of my favorite Elixir features: placing it somewhere in a pipe of function calls outputs all the intermediate results. It’s great for debugging parsing code. Like this: meta\_date = document …

---

## [Why \`throw/catch\` is discouraged for control flow in favor of \`with\` special form?](https://forum.elixirforum.com/t/why-throw-catch-is-discouraged-for-control-flow-in-favor-of-with-special-form/51600)

<div class="topic-metadata">

**Author:** [@RudManusachi](https://forum.elixirforum.com/u/RudManusachi)\
**Replies:** 20\
**Last updated:** [November 16, 2022, 11:56am UTC](https://forum.elixirforum.com/t/why-throw-catch-is-discouraged-for-control-flow-in-favor-of-with-special-form/51600 "2022-11-16T11:56:36Z")

</div>

Hi there :wave: ! This week at CodeBEAM I had a chance to meet an amazing crowd and participate in some interesting conversations. One of them was with more experienced with erlang folks discussing new maybe\_expr in OTP…

---

## [How would you accumulate errors?](https://forum.elixirforum.com/t/how-would-you-accumulate-errors/48205)

<div class="topic-metadata">

**Author:** [@mattfara50](https://forum.elixirforum.com/u/mattfara50)\
**Replies:** 7\
**Last updated:** [June 6, 2022, 1:22am UTC](https://forum.elixirforum.com/t/how-would-you-accumulate-errors/48205 "2022-06-06T01:22:04Z")

</div>

In “Elixir in Action”, there is an example about using pattern matching with the “with” special form: with {:ok, login} \<- extract\_login(user), {:ok, email} \<- extract\_mail(user), {:ok, pw} \<- …

---

## [Proper way of handling simple booleans in \`with\` statement](https://forum.elixirforum.com/t/proper-way-of-handling-simple-booleans-in-with-statement/45556)

<div class="topic-metadata">

**Author:** [@josefrichter](https://forum.elixirforum.com/u/josefrichter)\
**Replies:** 39\
**Last updated:** [May 4, 2022, 7:10pm UTC](https://forum.elixirforum.com/t/proper-way-of-handling-simple-booleans-in-with-statement/45556 "2022-05-04T19:10:44Z")

</div>

Say I have a bunch of simple boolean conditions I want to check in a with statement, something like with true \<- is\_integer(foo), true \<- rem(foo,2) == 0, true \<- foo \> 50 do # do something else # distinguish …

---

## [Easiest way to find the function that didn't match in the \`with\` statement](https://forum.elixirforum.com/t/easiest-way-to-find-the-function-that-didnt-match-in-the-with-statement/46032)

<div class="topic-metadata">

**Author:** [@tomekowal](https://forum.elixirforum.com/u/tomekowal)\
**Replies:** 10\
**Last updated:** [April 4, 2022, 5:42pm UTC](https://forum.elixirforum.com/t/easiest-way-to-find-the-function-that-didnt-match-in-the-with-statement/46032 "2022-04-04T17:42:28Z")

</div>

I have code like this (but bigger) defmodule WithStacktrace do def hello do with {:ok, \_value} \<- {:ok, 1}, {:ok, value} \<- :wrong\_shape do {:ok, value} else {:error, e} -\> {:error, e} en…

---

## [\`with\` construct usage question - variables in \`else\`](https://forum.elixirforum.com/t/with-construct-usage-question-variables-in-else/43754)

<div class="topic-metadata">

**Author:** [@silverdr](https://forum.elixirforum.com/u/silverdr)\
**Replies:** 11\
**Last updated:** [November 12, 2021, 10:29am UTC](https://forum.elixirforum.com/t/with-construct-usage-question-variables-in-else/43754 "2021-11-12T10:29:05Z")

</div>

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 str…

---

## [Avoiding long \`with\` blocks](https://forum.elixirforum.com/t/avoiding-long-with-blocks/42921)

<div class="topic-metadata">

**Author:** [@jonathan-scholbach](https://forum.elixirforum.com/u/jonathan-scholbach)\
**Replies:** 3\
**Last updated:** [October 7, 2021, 2:51pm UTC](https://forum.elixirforum.com/t/avoiding-long-with-blocks/42921 "2021-10-07T14:51:44Z")

</div>

I find myself using a lot of very long with-statements. The functions in the with pipeline return {:ok, result} or some {:error, :error\_type, additional\_error\_information} It could look somewhat like this: with \\ {:o…

---

## [Function body is nested too deep](https://forum.elixirforum.com/t/function-body-is-nested-too-deep/41841)

<div class="topic-metadata">

**Author:** [@siddhant3030](https://forum.elixirforum.com/u/siddhant3030)\
**Replies:** 4\
**Last updated:** [August 22, 2021, 1:51pm UTC](https://forum.elixirforum.com/t/function-body-is-nested-too-deep/41841 "2021-08-22T13:51:13Z")

</div>

I have a case statement that is failing in credo. Giving me this error “Function body is nested too deep” I know we can use “with” here but I’m failing to use it correctly case NaiveDateTime.from\_iso8601(valid\_time) …

---

## [Error handling with "with"](https://forum.elixirforum.com/t/error-handling-with-with/41519)

<div class="topic-metadata">

**Author:** [@intercaetera](https://forum.elixirforum.com/u/intercaetera)\
**Replies:** 2\
**Last updated:** [August 7, 2021, 2:24am UTC](https://forum.elixirforum.com/t/error-handling-with-with/41519 "2021-08-07T02:24:55Z")

</div>

Sort of a newbie code review question, but I’m curious if there’s a way to work around this. I’m still trying to wrap my head around good practices for error tuples. The precise code is in a Phoenix app but it could well…

---

## [Why was equal sign used in with statement?](https://forum.elixirforum.com/t/why-was-equal-sign-used-in-with-statement/41010)

<div class="topic-metadata">

**Author:** [@woohaaha](https://forum.elixirforum.com/u/woohaaha)\
**Replies:** 12\
**Last updated:** [July 14, 2021, 2:51pm UTC](https://forum.elixirforum.com/t/why-was-equal-sign-used-in-with-statement/41010 "2021-07-14T14:51:20Z")

</div>

I read an article on the Dockyard blog and I’m wondering why they used an equal sign with a with statement. The code example is: defmodule CMS.PageController do def update(conn, %{"id" =\> id, "page" =\> page\_para…

---

## [Advice for working around my \_only\_ problem with "with" expressions - accessing success results](https://forum.elixirforum.com/t/advice-for-working-around-my-only-problem-with-with-expressions-accessing-success-results/33720)

<div class="topic-metadata">

**Author:** [@slouchpie](https://forum.elixirforum.com/u/slouchpie)\
**Replies:** 13\
**Last updated:** [March 1, 2021, 2:53pm UTC](https://forum.elixirforum.com/t/advice-for-working-around-my-only-problem-with-with-expressions-accessing-success-results/33720 "2021-03-01T14:53:52Z")

</div>

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} -\> {:…

---

## [Weird/incorrect output from a with statement](https://forum.elixirforum.com/t/weird-incorrect-output-from-a-with-statement/36102)

<div class="topic-metadata">

**Author:** [@axelson](https://forum.elixirforum.com/u/axelson)\
**Replies:** 13\
**Last updated:** [December 9, 2020, 11:01pm UTC](https://forum.elixirforum.com/t/weird-incorrect-output-from-a-with-statement/36102 "2020-12-09T23:01:42Z")

</div>

This is a question that originated on the Elixir Slack from David Billskog and I have made only minor changes to the module. Given this module: defmodule Dummy do def execute(x) do with var\_a \<- get\_a(x), …

---

## [How can I access variable defined in "With" statement from its "else" block?](https://forum.elixirforum.com/t/how-can-i-access-variable-defined-in-with-statement-from-its-else-block/12955)

<div class="topic-metadata">

**Author:** [@Kurisu](https://forum.elixirforum.com/u/Kurisu)\
**Replies:** 12\
**Last updated:** [July 25, 2020, 12:25am UTC](https://forum.elixirforum.com/t/how-can-i-access-variable-defined-in-with-statement-from-its-else-block/12955 "2020-07-25T00:25:00Z")

</div>

Hello forum! I’m trying to retrieve an updated conn, returned in the first line of a “with” statement, when second line failed. But I’m getting some error that says new\_conn variable doesn’t exist. In the “do” block it …

---

## [Should one use pipes or with?](https://forum.elixirforum.com/t/should-one-use-pipes-or-with/21000)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 23\
**Last updated:** [May 10, 2020, 7:04am UTC](https://forum.elixirforum.com/t/should-one-use-pipes-or-with/21000 "2020-05-10T07:04:25Z")

</div>

Background Recently I have rediscovered the with statement to program in a more rail oriented way, and I must say I am loving it thus far. However in work, my colleagues prefer the usual pipeline oriented approach to ra…

---

## [I can't get Elixir error handling right](https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843)

<div class="topic-metadata">

**Author:** [@pmjoe](https://forum.elixirforum.com/u/pmjoe)\
**Replies:** 15\
**Last updated:** [July 4, 2019, 12:09am UTC](https://forum.elixirforum.com/t/i-cant-get-elixir-error-handling-right/15843 "2019-07-04T00:09:46Z")

</div>

In a with case, with many operations, how can i individually handle errors?

---

## ["non\_nil" shortcut inside with](https://forum.elixirforum.com/t/non-nil-shortcut-inside-with/22614)

<div class="topic-metadata">

**Author:** [@alexiss](https://forum.elixirforum.com/u/alexiss)\
**Replies:** 6\
**Last updated:** [May 22, 2019, 6:15pm UTC](https://forum.elixirforum.com/t/non-nil-shortcut-inside-with/22614 "2019-05-22T18:15:19Z")

</div>

In my practice I often write something like this: with value when not is\_nil(value) \<- some\_fun() do ... end What about something like shortcut for this frequently used case? with non\_nil(value) \<- some\_fun() do .…

---

## [With/1 design patterns](https://forum.elixirforum.com/t/with-1-design-patterns/15639)

<div class="topic-metadata">

**Author:** [@yourpalal](https://forum.elixirforum.com/u/yourpalal)\
**Replies:** 12\
**Last updated:** [August 7, 2018, 1:29pm UTC](https://forum.elixirforum.com/t/with-1-design-patterns/15639 "2018-08-07T13:29:49Z")

</div>

When using with/1 to handle a bunch of matches and things that return {:ok, \_} or :error or {:error, \_} I often need to differentiate between errors, and I’ll wrap the individual matches in tuples, like this: with( …
