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

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

Re: [External] : Why I cannot use this variable in macro call from funct


From: tomas
Subject: Re: [External] : Why I cannot use this variable in macro call from function?
Date: Tue, 8 Jun 2021 20:31:38 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Tue, Jun 08, 2021 at 08:14:08PM +0300, Jean Louis wrote:
> Major problem is that this does not behave how I expect it to behave:
> 
> (defvar (intern "my-function-123") nil)
> 
> (type-of 'my-function-123) ⇒ symbol
> 
> NOT WORKING:
> (defvar 'my-function-123 nil)

Read the docs: defvar is a special form, not a function. Its arguments 
**don't get evaluated**. That's something you don't want special forms
to do.

And 'my-function-123 *is*not*a*symbol*. It is a handy shorthand for
(quote my-function-123).

So what your (defvar...) above is seeing is:

  (defvar (quote my-function123) nil)

and it complains.

Same for the (intern...) attempt: your defvar sees the whole two-element
S-expression (intern "my-function-123") and says:

> (defvar (intern "my-function-123") nil):
> 
> Debugger entered--Lisp error: (wrong-type-argument symbolp (intern 
> "my-function-123"))
>   boundp((intern "my-function-123"))
>   elisp--eval-defun-1((defvar (intern "my-function-123") 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)

"HEY! I have been built to accept a symbol as my first arg, and am
getting this funny list '(intern "my-function-123). Help!"

> Though it says that after `defvar' should come SYMBOL
> 
> defvar is a special form in ‘src/eval.c’.
> 
> (defvar SYMBOL &optional INITVALUE DOCSTRING)
> 
>   Probably introduced at or before Emacs version 1.5.
> 
> Define SYMBOL as a variable, and return SYMBOL.

Exactly. A SYMBOL and not a form.

Read again the chapter on special forms from the fine elisp manual.
Please, really do.

  10.2.7 Special Forms
  --------------------

  A “special form” is a primitive function specially marked so that its
  arguments are not all evaluated.  Most special forms define control
  structures or perform variable bindings—things which functions cannot
  do.

We are so accustomed to things being evaluated from the inside-out
that we read 'foo as "the symbol foo", and (+ 3 2) as 5. They are
not. They are expressions which (eventually) evaluate to the symbol
foo and to 5, if we let them. Macros and special forms don't let
them, to give you, the programmer, the chance to intervene before.

The price you pay is that it's sometimes difficult to wrap one's
head around it.

Someone said upthread macros are harder than functions. I think that's
spot-on.

That said, they are different tools, too. You could turn a screw with
a bandsaw, but it's generally not recommended.

Cheers
 - t

Attachment: signature.asc
Description: Digital signature


reply via email to

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