simonelnahas
Using `with` as the Elixir replacement for `let` in Lisp
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.
with a <- Map.fetch(m, :apple) do
# code using a
end
(let [a (:apple m)]
# code using a
)
What do you think?
Most Liked
sodapopcan
I think it really just comes down to idioms. I know let from haskell/clojure (not that I use any of those languages extensively or even at all beyond toy stuff) but not having it in Elixir, which I at least read every day, has not ever not even once ever been any kind of problem. Can you give an example of where it would prevent a bug? Otherwise, using with denotes some kind of side-effect or procedure is about to take place which is a nice readability (really scannability) hint. Overloading it for variable scoping muddies this.
zachallaun
To answer the original question, I think it is unidiomatic, of marginal (or negative) utility, would be confusing to experienced Elixir programmers, and code review would not allow it through in any of my own projects.
With that said, your code, your rules! If it helps you to transition from Clojure, then go for it! I’d be surprised to find you still using with this way once you’ve written as many lines of Elixir, however.
dimitarvp
I don’t disagree on the premise but let’s not act like code is a cat that just runs off and does random villainy all over the place by itself. If you access a variable 20 lines below that you really shouldn’t then you IMO have much bigger problems – like not knowing what your own code is doing (maybe you inherited it?) and you should add a lot of tests. Ideally, also reduce the line count of your functions by extracting parts.
I’m no stranger to enforcing policies regardless of whether they seem best-suited for the situation. Do what you feel is right but do your best to write idiomatic and readable Elixir.
Popular in Discussions
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









