simonelnahas

simonelnahas

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?

Showing Posts 1 to 10

dimitarvp

dimitarvp

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?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What does “let” do if the map does not have that key?

bunnylushington

bunnylushington

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.

simonelnahas

simonelnahas OP

@dimitarvp

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.

@benwilson512

What does “let” do if the map does not have that key?

No error handling as is the case with with.

@bunnylushington

Overloading with (not Elixir’s most obvious construct) with additional semantics seems like a maintenance issue waiting to happen.

No overloading is needed, this already works.

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.

simonelnahas

simonelnahas OP

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 :slight_smile:

bunnylushington

bunnylushington

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.

shishini

shishini

You can define a function inside let in clojure, just use the anonymous function syntax
(clojure code below )

(let [add (fn [ v t] (+ v t)) x 43 b 56] (add x b))
;; or
(let [add #(+ %1 %2) x 42 b 4] (add x b))
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.

harrisi

harrisi

If you only need a in the with, it seems this would be better as a function. Instead of

def foo(%{} = m) do
  with a <- Map.fetch(m, :apple) do
    # code using a
  end
  # maybe do some other stuff unrelated to a
end

You could do

def foo(%{} = m) do
  do_a(m)
  # ...
end

defp do_a(%{} = m) do
  a = Map.fetch(m, :apple)
  # do stuff with a
end

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews