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
If your goal is to reduce the scope of variables then yes, with will work okay for it. Personally I’m not sure I see the value though. Any specific goal or use case that you have in mind?
Apparently it’s true: you can write Fortran in any language!
I don’t see the point. Overloading with (not Elixir’s most obvious construct) with additional semantics seems like a maintenance issue waiting to happen. I also think there’s lossage in an instance like:
with {:ok, a} <- Map.fetch(map, :non_key)
I would really want that to crash (using faux let semantics anyway). Wrapping it up in with hides the problem.
If your goal is to reduce the scope of variables then yes, with will work okay for it. Personally I’m not sure I see the value though. Any specific goal or use case that you have in mind?
Basically the same as let in Clojure, to assign a variable that can only be used within the scope. It may sound rudimentary for people used to Elixir, but they greatly reduces mistakes due to scope pollution in my opinion.
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.
Interestingly in Clojure (and other Lisp), it is actually only possible to do variable assignment inside a let expression. Although it seems quite limiting at first I find it improves the code quality in the end
I think that’s not true. In CL you have defvar and defparameter (and plain old setf) for variable assignment. Emacs lisp adds defcustom to that list. I’m not that knowledgable about Clojure but I thought def was available for global variables? The use of let is necessary to declare a local variable that may shadow a global; this isn’t required in Elixir.
It might be that I write my lisp wrong but nearly always my functions look like
(defun my-fun ()
(let (...)
... function body ... ))
which if you squint a little (and make that a let*) is what an Elixir function looks like, modulo the keyword.
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.
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.