unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Question about cl-flet and cl-letf
@ 2023-10-26 20:10 Arash Esbati
  2023-10-26 21:53 ` Michael Heerdegen
  0 siblings, 1 reply; 6+ messages in thread
From: Arash Esbati @ 2023-10-26 20:10 UTC (permalink / raw)
  To: emacs-help

Hi all,

my understanding of the manual and docstring of the macros mentioned
above is that these two pieces of code should be equivalent:

  (cl-flet ((y-or-n-p #'always))
    (while (...)
      BODY))

  (cl-letf (((symbol-function 'y-or-n-p) #'always))
    (while (...)
      BODY))

I have an use-case where they are not.  Before getting into details
about the function (which is long) and trying to come up with a reduced
version: Am I correct with the assumption above?  Currently, my function
works with `cl-left' as expected and with `cl-flet' not, i.e.,
`y-or-no-p' returns t inside BODY with `cl-letf' and not with `cl-flet'.

I'd appreciate if somebody would enlighten me.

Best, Arash



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

* Re: Question about cl-flet and cl-letf
  2023-10-26 20:10 Question about cl-flet and cl-letf Arash Esbati
@ 2023-10-26 21:53 ` Michael Heerdegen
  2023-10-28 21:07   ` Arash Esbati
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Heerdegen @ 2023-10-26 21:53 UTC (permalink / raw)
  To: help-gnu-emacs

Arash Esbati <arash@gnu.org> writes:

> Hi all,
>
> my understanding of the manual and docstring of the macros mentioned
> above is that these two pieces of code should be equivalent:
>
>   (cl-flet ((y-or-n-p #'always))
>     (while (...)
>       BODY))
>
>   (cl-letf (((symbol-function 'y-or-n-p) #'always))
>     (while (...)
>       BODY))
>
> I have an use-case where they are not.  Before getting into details
> about the function (which is long) and trying to come up with a reduced
> version: Am I correct with the assumption above?

No - they very different.  `cl-flet' creates lexical bindings.  Your
`cl-letf' call OTOH temporarily changes the function binding of the
symbol `y-or-n-p' - which more or less gives you dynamical binding.

Michael.




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

* Re: Question about cl-flet and cl-letf
  2023-10-26 21:53 ` Michael Heerdegen
@ 2023-10-28 21:07   ` Arash Esbati
  2023-10-29  0:14     ` Michael Heerdegen
  0 siblings, 1 reply; 6+ messages in thread
From: Arash Esbati @ 2023-10-28 21:07 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> No - they very different.  `cl-flet' creates lexical bindings.  Your
> `cl-letf' call OTOH temporarily changes the function binding of the
> symbol `y-or-n-p' - which more or less gives you dynamical binding.

Thanks for your response.  I basically want to temporarily make
`y-or-n-p' act like `always'; and from what I read in the docstrings,
both version should work, but `cl-flet' does not.  Are the bindings
relevant in this case?  Sorry if I'm missing the obvious here.

Best, Arash



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

* Re: Question about cl-flet and cl-letf
  2023-10-28 21:07   ` Arash Esbati
@ 2023-10-29  0:14     ` Michael Heerdegen
  2023-10-29  6:19       ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Heerdegen @ 2023-10-29  0:14 UTC (permalink / raw)
  To: help-gnu-emacs

Arash Esbati <arash@gnu.org> writes:

> Michael Heerdegen <michael_heerdegen@web.de> writes:
>
> > No - they very different.  `cl-flet' creates lexical bindings.  Your
> > `cl-letf' call OTOH temporarily changes the function binding of the
> > symbol `y-or-n-p' - which more or less gives you dynamical binding.
>
> Thanks for your response.  I basically want to temporarily make
> `y-or-n-p' act like `always'; and from what I read in the docstrings,
> both version should work, but `cl-flet' does not.  Are the bindings
> relevant in this case?

Yes.  Please read about scoping rules:

  (info "(elisp) Variable Scoping")

the analogue rules apply for function bindings.

When the call of `y-or-n-p' does not occur textually inside the
`cl-flet' form, it will not be affected by a lexical function binding.


Michael.




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

* Re: Question about cl-flet and cl-letf
  2023-10-29  0:14     ` Michael Heerdegen
@ 2023-10-29  6:19       ` tomas
  2023-10-31  9:38         ` Arash Esbati
  0 siblings, 1 reply; 6+ messages in thread
From: tomas @ 2023-10-29  6:19 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1258 bytes --]

On Sun, Oct 29, 2023 at 02:14:10AM +0200, Michael Heerdegen wrote:
> Arash Esbati <arash@gnu.org> writes:
> 
> > Michael Heerdegen <michael_heerdegen@web.de> writes:
> >
> > > No - they very different.  `cl-flet' creates lexical bindings.  Your
> > > `cl-letf' call OTOH temporarily changes the function binding of the
> > > symbol `y-or-n-p' - which more or less gives you dynamical binding.
> >
> > Thanks for your response.  I basically want to temporarily make
> > `y-or-n-p' act like `always'; and from what I read in the docstrings,
> > both version should work, but `cl-flet' does not.  Are the bindings
> > relevant in this case?
> 
> Yes.  Please read about scoping rules:
> 
>   (info "(elisp) Variable Scoping")
> 
> the analogue rules apply for function bindings.
> 
> When the call of `y-or-n-p' does not occur textually inside the
> `cl-flet' form, it will not be affected by a lexical function binding.

It can be confusing. Look at it this way: y-or-no-p is most probably
not called directly in your (while ...) construct (which is a
special form) but from some functions called from there.

So it's not in your lexical environment, but on somewhere else's.
So a dynamic binding does the trick.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Question about cl-flet and cl-letf
  2023-10-29  6:19       ` tomas
@ 2023-10-31  9:38         ` Arash Esbati
  0 siblings, 0 replies; 6+ messages in thread
From: Arash Esbati @ 2023-10-31  9:38 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

<tomas@tuxteam.de> writes:

> It can be confusing. Look at it this way: y-or-no-p is most probably
> not called directly in your (while ...) construct (which is a
> special form) but from some functions called from there.
>
> So it's not in your lexical environment, but on somewhere else's.
> So a dynamic binding does the trick.

Thanks for your response.  Now I grok what's happening.

Best, Arash



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

end of thread, other threads:[~2023-10-31  9:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-26 20:10 Question about cl-flet and cl-letf Arash Esbati
2023-10-26 21:53 ` Michael Heerdegen
2023-10-28 21:07   ` Arash Esbati
2023-10-29  0:14     ` Michael Heerdegen
2023-10-29  6:19       ` tomas
2023-10-31  9:38         ` Arash Esbati

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).