emacs-devel
[Top][All Lists]
Advanced

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

Re: Conditional binding and testing of `lexical-binding'


From: LdBeth
Subject: Re: Conditional binding and testing of `lexical-binding'
Date: Sun, 02 Jan 2022 20:36:26 +0800
User-agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (Gojō) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.2 (x86_64-apple-darwin18.7.0) MULE/6.0 (HANACHIRUSATO)

>>>>> In 
>>>>> <SJ0PR10MB5488D4372A6BCE46CF82D0E1F3479@SJ0PR10MB5488.namprd10.prod.outlook.com>
>>>>>  
>>>>>   Drew Adams <drew.adams@oracle.com> wrote:


>  (if (and (boundp 'lexical-binding)
>           lexical-binding)
>      (lambda (y) (something x y))
>    `(lambda (y) (something ',x y)))

You may use `static-if' to benifit from
byte-compiling.

> But my question is really about conditionally
> _setting_ `lexical-binding', so it can be tested.

I think the "lispy" way is to use:

(provide 'lexical-binding)

and use `featurep' to test it.

> Putting it in `Local Variables' at the end of
> a file, and using `eval' to set its value (e.g.
> conditionally, depending on Emacs version or
> `boundp' or whatever), has no effect.  As the
> doc says, we must instead set it in the first
> file line.

> And trying to set it conditionally this way
> in a file apparently has no effect either:

>  (ignore-errors (eval '(setq lexical-binding t)
>                       t))

> (That uses the 2-arg version of `eval', so it
> raises an error in older Emacs versions; hence
> the `ignore-errors'.)
> ___

> It works to just put it in the first file line:

>  ;;; ....... -*- lexical-binding:t -*-

> In Emacs versions where that variable doesn't
> exist, this apparently has no effect - the var
> continues not to exist after the file's loaded.

> [I don't see that the doc says that the var is
> set only if it already exists (or whatever the
> actual criterion is).  I was expecting that
> that declaration would set `local-variables' to
> `t' in older Emacs versions also, so I didn't
> try it till after (unsuccessfully) trying other 
> conditional approaches.  Shouldn't something be
> said about this in the doc?]

A more reliable way is to test

(static-if (assoc 'lexical-binding (buffer-local-variables))
   (provide 'lexical-binding))

instead of doing `boundp' test.

> Is this the thing to do?  If not, what advice
> do you have for adapting a library to use
> lexical binding when available (Emacs 24+) but
> to also work when it's unavailable (Emacs < 24)?

> [The doc just tells you how to convert code to
> use lexical binding.  I see nothing about how
> to code compatibly for old and new Emacs.]

Well, I think it's fine to just keep that library as-is.
Even in the current lastest release there are still many
builtin packages are not converted to using lexical binding
at all. Just name a few I know, supercite, enriched-mode 

Best,
LDB



reply via email to

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