all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Function value changes after running function?
@ 2021-02-05  2:41 okamsn+emacs-help
  2021-02-05  3:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-02-05 15:24 ` Stefan Monnier
  0 siblings, 2 replies; 6+ messages in thread
From: okamsn+emacs-help @ 2021-02-05  2:41 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

On Emacs 27.1, I've learned that a function using a macro I wrote raises 
an error after running the function a second time.

The function is

     (defun mu4e:other-path ()
       "Return load-path for mu4e.
     This assumes that you're on guix."
       (loopy (with (regexp "Documents")
     	       (base-dir (f-full "~/")))
     	 ((list file (directory-files base-dir))
     	  (expr full-path (f-expand file base-dir)))))

and running it once from my home directory returns nil, as expected. 
However, if run again, it raises an error

     ***  Symbol’s value as variable is void: base-dir

I've found after evaluating the function definition, `symbol-function' 
reports the definition as

     (closure
      (t)
      nil "Return load-path for mu4e.
     This assumes that you're on guix."
      (loopy
       (with
        (regexp "Documents")
        (base-dir
         (f-full "~/")))
       ((list file
              (directory-files base-dir))
        (expr full-path
              (f-expand file base-dir)))))

where `base-dir` is defined, but if I run it again, the function 
definition is reported again as

     (closure
      (t)
      nil "Return load-path for mu4e.
     This assumes that you're on guix."
      (loopy
       (with
        (regexp "Documents"))
       ((list file
              (directory-files base-dir))
        (expr full-path
              (f-expand file base-dir)))))

where `base-dir` is no longer defined.

Why would the function definition be changed after running the function? 
  Is that a known problem with macros, or with Emacs 27.1?  I haven't 
seen this on Emacs 28.

Thank you.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function value changes after running function?
  2021-02-05  2:41 Function value changes after running function? okamsn+emacs-help
@ 2021-02-05  3:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-02-05 15:34   ` okamsn+emacs-help
  2021-02-05 15:24 ` Stefan Monnier
  1 sibling, 1 reply; 6+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-02-05  3:16 UTC (permalink / raw)
  To: help-gnu-emacs

okamsn+emacs-help wrote:

>     (defun mu4e:other-path ()
>       "Return load-path for mu4e.
>     This assumes that you're on guix."
>       (loopy (with (regexp "Documents")
>     	       (base-dir (f-full "~/")))
>     	 ((list file (directory-files base-dir))
>     	  (expr full-path (f-expand file base-dir)))))

Uhm, what language is that? You get a new Elisp with Guix
as well?

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




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function value changes after running function?
  2021-02-05  2:41 Function value changes after running function? okamsn+emacs-help
  2021-02-05  3:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-02-05 15:24 ` Stefan Monnier
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2021-02-05 15:24 UTC (permalink / raw)
  To: help-gnu-emacs

> Why would the function definition be changed after running the function? 

Sounds like a bug in `loopy` (which appears to be a macro and which
probably does something like `nreverse` on some part of its arguments).


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function value changes after running function?
  2021-02-05  3:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-02-05 15:34   ` okamsn+emacs-help
  2021-02-05 15:48     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: okamsn+emacs-help @ 2021-02-05 15:34 UTC (permalink / raw)
  To: help-gnu-emacs

On 2/4/21 10:16 PM, Emanuel Berg via Users list for the GNU Emacs text 
editor wrote:
> 
> okamsn+emacs-help wrote:
> 
>>      (defun mu4e:other-path ()
>>        "Return load-path for mu4e.
>>      This assumes that you're on guix."
>>        (loopy (with (regexp "Documents")
>>      	       (base-dir (f-full "~/")))
>>      	 ((list file (directory-files base-dir))
>>      	  (expr full-path (f-expand file base-dir)))))
> 
> Uhm, what language is that? You get a new Elisp with Guix
> as well?
> 
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
> 
> 

No, I wrote a macro like `cl-loop` here:

https://github.com/okamsn/loopy

I received a bug report that said that the above function would raise an 
error after running a second time here:

https://github.com/okamsn/loopy/issues/17

For some reason, the value returned by `symbol-function` changes after 
the function is invoked the first time. Just using the macro itself does 
not signal an error.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function value changes after running function?
  2021-02-05 15:34   ` okamsn+emacs-help
@ 2021-02-05 15:48     ` Stefan Monnier
  2021-02-05 17:27       ` Okam
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2021-02-05 15:48 UTC (permalink / raw)
  To: help-gnu-emacs

> No, I wrote a macro like `cl-loop` here:
>
> https://github.com/okamsn/loopy
>
> I received a bug report that said that the above function would raise an 
> error after running a second time here:

In the current situation, the problem is:

    (setq loopy--main-body (nreverse loopy--main-body)
          loopy--with-vars (nreverse loopy--with-vars))

which reverses the "with vars" received as arguments.
If you replace `nreverse` with `reverse` here, this particular problem
should disappear.

This said, this `nreverse` reverses the order of the bindings in (with ....)
which maybe isn't what you intended (I suspect that the `nreverse` here
is meant to reverse the bindings added to `loopy--with-vars` via
`push`).
So maybe a better solution is to replace

        (setq loopy--with-vars (cdr arg)))

with

        (setq loopy--with-vars (reverse (cdr arg))))

so that the subsequent `nreverse` puts them back in the original order.


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function value changes after running function?
  2021-02-05 15:48     ` Stefan Monnier
@ 2021-02-05 17:27       ` Okam
  0 siblings, 0 replies; 6+ messages in thread
From: Okam @ 2021-02-05 17:27 UTC (permalink / raw)
  To: help-gnu-emacs

On 2/5/21 10:48 AM, Stefan Monnier wrote:
> 
>> No, I wrote a macro like `cl-loop` here:
>>
>> https://github.com/okamsn/loopy
>>
>> I received a bug report that said that the above function would raise an
>> error after running a second time here:
> 
> In the current situation, the problem is:
> 
>      (setq loopy--main-body (nreverse loopy--main-body)
>            loopy--with-vars (nreverse loopy--with-vars))
> 
> which reverses the "with vars" received as arguments.
> If you replace `nreverse` with `reverse` here, this particular problem
> should disappear.
> 
> This said, this `nreverse` reverses the order of the bindings in (with ....)
> which maybe isn't what you intended (I suspect that the `nreverse` here
> is meant to reverse the bindings added to `loopy--with-vars` via
> `push`).
> So maybe a better solution is to replace
> 
>          (setq loopy--with-vars (cdr arg)))
> 
> with
> 
>          (setq loopy--with-vars (reverse (cdr arg))))
> 
> so that the subsequent `nreverse` puts them back in the original order.
> 
> 
>          Stefan
> 
> 

Thank you.  That does prevent the error, but would you please explain 
(or point me to where I could learn) how `nreverse` causes this error?

Why would invoking the function a second time change what the function 
does? The return value of `symbol-function` makes it look like the 
function passes different arguments to the macro after the first 
invocation. Is that a misleading return value, or is that actually 
happening? If that is what happens, why does that happen?




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-02-05 17:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-05  2:41 Function value changes after running function? okamsn+emacs-help
2021-02-05  3:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-02-05 15:34   ` okamsn+emacs-help
2021-02-05 15:48     ` Stefan Monnier
2021-02-05 17:27       ` Okam
2021-02-05 15:24 ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.