help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: let, let*, oh, why [was: Elisp - Function returning a list]


From: Emanuel Berg
Subject: Re: let, let*, oh, why [was: Elisp - Function returning a list]
Date: Wed, 16 Dec 2020 15:13:21 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

tomas wrote:

> What "upwards" means depends on whether you are under
> dynamic scope (traditional) or lexical scope (more modern,
> recommended almost always).

Indeed, put this topmost in your Elisp files:

;;; -*- lexical-binding: t -*-

Then use `let' and `let*'.

Write code as one intuitively does, based on functions that do
stuff and/or return stuff.

When done, byte compile.

Simple simple simple. Actually it is much easier to do right
than to do wrong in this case.

But ... to be fair it also works like this:

(defvar some-var)
(setq some-var 0) ; 0

(defun test-var ()
  (let ((some-var 1))
    (setq some-var 2)
    some-var) )

(test-var) ; 2

some-var ; 0

But it is still ugly and error prone. If you make a typo it
won't tell you when you evaluate the function:

(defvar some-var)
(setq some-var 0) ; 0

(defun test-var ()
  (let ((some-var 1))
    (setq some-vari 2) ; oups
    some-var) )

(test-var) ; 1 ???

some-var ; 0

But the byte-compiler will:

  In test-var:
  geh.el:210:11: Warning: assignment to free variable ‘some-vari’

So, better to do all computation in the `let's, then use them
in the body. Yes, nest lets where needed... let is supposedly
parallel while let* is sequential but in practice it most
often means with let* you can see the vars and their values
later or in the varlist...

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]