simonelnahas

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

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

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

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.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement