Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in lexical binding?
From: Phil Sainty <[email-masked]>
To: Jean Louis <[email-masked]>
Cc: [email-masked]
Date: Sun, 20 Jun 2021 00:49:14 +1200
List-Id: <debbugs-submit.debbugs.gnu.org>

On 2021-06-19 23:39, Jean Louis wrote:
> When I evaluate following it is not detecting variable `direction'
> under lexical bindings:
> 
> (defun verify-direction (timestamp &optional direction)
>   (cond ((bound-and-true-p direction) (message "OK"))))
> 
> (verify-direction "10:00" t) ⇒ nil
> 
> I would like to understand if it is intended to be so?
> 
> Or is this possible bug?

This is expected / not a bug (and so I'm closing it).

(bound-and-true-p VAR) is syntactic sugar for (and (boundp 'VAR) VAR)
which means that it works only for dynamic variables (i.e. variables
stored in the value slot of a symbol).

Under lexical binding, function arguments are lexical variables, which
means they are not stored in the symbol of that name, and hence cannot
be detected with `boundp'.

In your case there's no need to be checking whether `direction' is
"bound" -- whether it's lexical or dynamic, you already *know* that
it exists because it's an argument for the function in which you're
checking it.  All you want is the "true-p" part, which is implicit
in every value (either nil or non-nil).

Instead of "(bound-and-true-p direction)" just use "direction".

-Phil