unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: tomas@tuxteam.de
Cc: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Re: [SOLVED with `eval']: Why I cannot use this variable in macro call from function?
Date: Wed, 9 Jun 2021 17:39:25 +0300	[thread overview]
Message-ID: <YMDSnWIzbqWBQ5R7@protected.localdomain> (raw)
In-Reply-To: <20210609113353.GB4155@tuxteam.de>

;; -*- lexical-binding: t; -*-
* tomas@tuxteam.de <tomas@tuxteam.de> [2021-06-09 14:35]:
> On Wed, Jun 09, 2021 at 01:56:45PM +0300, Jean Louis wrote:
> > * tomas@tuxteam.de <tomas@tuxteam.de> [2021-06-09 11:54]:
> > > On Wed, Jun 09, 2021 at 11:22:38AM +0300, Jean Louis wrote:
> > > > * tomas@tuxteam.de <tomas@tuxteam.de> [2021-06-09 10:40]:
> > > > > You snipped the (for me) interesting part: did you notice how
> > > > > `eval' jumps over the local declaration?
> > > > 
> > > > Do you mean variables within `let'?
> > > 
> > > Yes, it doesn't see them :)
> > 
> > Maybe in theory it does not see, but in reality it does see it as
> > `list' is evaluated before `eval', so the interned `rcd-symbol' and
> > variable `description' they get evaluated before `eval'.

> That sentence doesn't make any sense to me. It does or it doesn't.

Well, I get confused too, you said that it does not see, but it is
obvious that it does see.

> I propose to you the next experiment:
> 
> * experiment 3
> 
>   (let ()
>     (let ((x 42))
>       (eval '(progn (setq x 43) (message "in eval: x is %S" x)))
>       (message "inner let: x is %S" x))
>     (message "outer let: x is %S" x))
> 
> (you might have to switch to the *Messages* buffer to see all three
> messages).

The outer scope does not see the inner scope.

Then the buffer *Backtrace* jumps up:

Debugger entered--Lisp error: (void-variable x)
  (message "outer let: x is %S" x)
  (let nil (let ((x 42)) (eval '(progn (setq x 43) (message "in eval: x is %S" x))) (message "inner let: x is %S" x)) (message "outer let: x is %S" x))
  eval((let nil (let ((x 42)) (eval '(progn (setq x 43) (message "in eval: x is %S" x))) (message "inner let: x is %S" x)) (message "outer let: x is %S" x)) nil)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

> What are the results? Do they correspond to your expectations? If
> not, why not?

I did not have any expectations for that piece of code and that
one does not generate new global variables to return the symbol
for history, which is what I need, and what is solved with `eval'
nicely.

Look here:

(let ((x 42))
  (eval (list 'progn (setq x 43) (message "in eval: x is %S" x)))) ⇒ "in eval: x is 43"

that obviously does work nicely as the list gets evaluated before eval
receives it. At least I assume it is so, according to learning about
the LISP in general.

In your example you have given pure data to `eval' in which case the
variables within data don't get expanded.

There is other way to expand them but using `list':

(let ((x 43))
   (eval `(+ 2 ,x))) ⇒ 45

It seems that this works too:

(let ((x 43))
   (eval '(+ 3 x))) ⇒ 46

While this will not work:

(let ()
(let ((x 43))
  (eval '(+ 3 x)))
x)

but this will also not work and is not related to `eval':

(let ()
(let ((x 43))
  x)
x)

As how I see that code above from you it is by its meaning
similar to that one above.

By the way, `eval' is powerful LISP function in general, it
obviously has its uses when nothing else works.

Example here below is when Emacs Lisp is read from a
database. User can define a type of a page and how to process the
page, for example with (rcd-markdown wrs-text) which symbolic
expression is fetched from the database. Of course, by allowing
user to define any Emacs Lisp into database there are security
issues and those are solved with permissions.

rcd-cf.el:6444:    (eval (car (read-from-string (rcd-db-get-entry "pagetypes" "pagetypes_elisp" type cf-db))))))

For some small functions I use `eval':
rcd-devel-utilities.el:57:  (let ((result (eval (elisp--preceding-sexp))))

(defun replace-last-sexp ()
  "Eval last sexp and replaces it in the buffer with its result."
  (interactive)
  (let ((result (eval (elisp--preceding-sexp))))
    (kill-sexp -1)
    (insert (format "%s" result))))

I can find I have been using it in the package `rcd-hash-edit.el'
very handy to edit visually a hash and dispatch the hash by
email, or other communication to collaborator:

(defun rcd-hash-create (symbol)
  "Create hash NAME.
Argument SYMBOL will be created in global space."
  (eval `(defvar ,(intern (symbol-name symbol)) (make-hash-table :test 'equal))))

I heard much talk of `eval' being bad, but that is not true
inherently.  function is powerful and one can feed data to
function and get it evaluated. When public is supplying input to
`eval' then there are some safety issues, but nothing so much
more than without eval. People accept input without `eval' and
they forget quoting or parsing and people enter into web servers
or intrude into systems.

Let's not forget, this is LISP, it is for `eval'-ing.

Yet, nothing from the above is not helping to make dynamically
generated global variables, do you know how to solve it in the
second run without `eval'?

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



  reply	other threads:[~2021-06-09 14:39 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08 13:10 Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 13:41 ` [External] : " Drew Adams
2021-06-08 14:47   ` Jean Louis
2021-06-08 17:14   ` Jean Louis
2021-06-08 18:31     ` tomas
2021-06-08 18:37       ` Oops function? tomas
2021-06-08 19:27       ` [SOLVED with `eval']: Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 20:03         ` tomas
2021-06-08 20:06           ` Sorry again tomas
2021-06-08 20:12           ` [SOLVED with `eval']: Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 20:23             ` tomas
2021-06-08 20:38               ` Jean Louis
2021-06-08 20:47               ` Jean Louis
2021-06-09  6:09                 ` tomas
2021-06-09  6:42                   ` Jean Louis
2021-06-09  6:51                     ` tomas
2021-06-09  7:03                       ` Jean Louis
2021-06-09  7:39                         ` tomas
2021-06-09  8:22                           ` Jean Louis
2021-06-09  8:54                             ` tomas
2021-06-09 10:56                               ` Jean Louis
2021-06-09 11:33                                 ` tomas
2021-06-09 14:39                                   ` Jean Louis [this message]
2021-06-09 16:41                                     ` tomas
2021-06-10  2:10                                     ` Robert Thorpe
2021-06-10  6:56                                       ` Jean Louis
2021-06-11  6:33                                         ` Robert Thorpe
2021-06-11  7:03                                           ` Jean Louis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YMDSnWIzbqWBQ5R7@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tomas@tuxteam.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).