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

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

bug#59786: Allowing arbitrary expressions in cl-labels


From: Stefan Monnier
Subject: bug#59786: Allowing arbitrary expressions in cl-labels
Date: Fri, 02 Dec 2022 14:44:13 -0500

Tags: patch

I have found some circumstances where I'd like to write things like:

    (cl-labels ((f1 (if blabla
                        (lambda (x) (do one thing))
                      (lambda (x) (do another thing))))
                (f2 (if bleble
                        (lambda (y) (do some thing))
                      (lambda (y) (do some other thing)))))
      ...)

I.e. define two, mutually-recursive functions, but where I want to
perform some computation before "building/returning" each function.

I could rewrite the above to

    (cl-labels ((f1 (x) (if blabla
                            (do one thing)
                          (do another thing)))
                (f2 (y) (if bleble
                            (do some thing)
                          (do some other thing))))
      ...)

but then the `if` tests are repeated at each call.
I could also rewrite it to

    (letrec ((f1 (if blabla
                     (lambda (x) (do one thing))
                   (lambda (x) (do another thing))))
             (f2 (if bleble
                     (lambda (y) (do some thing))
                   (lambda (y) (do some other thing)))))
      ...)

but then I have to use (funcall f1 ..) and (funcall f2 ...) instead of
just (f1 ...) and (f2 ...).  I could add a (cl-flet ((f1 f1) (f2 f2)) ...)
but that's inconvenient, especially because I'd have to add it in
various places.

So I'd like to propose to extend `cl-labels` in the same way that
`cl-flet` was extended to allow each function to be defined by an
expression that returns a function rather than by "args + body".

One option is to use the same approach as I used in `cl-flet`,
i.e. allow each binding to be either

    (FUNC ARGS BODY...)    the normal existing syntax
or
    (FUNC EXP)             the new syntax

After I introduced this in `cl-flet` it was pointed out that it was an
incompatible change since BODY... can be the empty list.  Another option
is to use a syntax like:

    (FUNC = EXP)           the new new syntax

which should not suffer from such incompatibility since ARGS should
never be of the form `=`.

The patch below uses this "new new" syntax (and adjusts `cl-flet` to
also support this new new syntax).  It still lacks a NEWS entry (as
well as updating the CL manual), but before I do that, I'd like to hear
what other people think,


        Stefan


 In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.16.0, Xaw3d scroll bars) of 2022-11-29 built on pastel
Repository revision: 4254a4a71d5d04cfcefaedfefe5d22af55650a6a
Repository branch: work
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure -C --enable-checking --enable-check-lisp-object-type --with-modules 
--with-cairo --with-tiff=ifavailable
 'CFLAGS=-Wall -g3 -Og -Wno-pointer-sign'
 PKG_CONFIG_PATH=/home/monnier/lib/pkgconfig'

Attachment: cl-labels.patch
Description: Text Data


reply via email to

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