[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [External] : Undefined behavior for the function ‘last’ or an error?
From: |
Drew Adams |
Subject: |
RE: [External] : Undefined behavior for the function ‘last’ or an error? |
Date: |
Mon, 1 Apr 2024 17:28:02 +0000 |
> The built-in Emacs Lisp function ‘last’ accepts
> a list as its argument:
>
> (last '(a b c)) => (c)
>
> It also (surprisingly) accepts a non-list as its argument:
>
> (last 'a) => a
> (last 3.14) => 3.14
> (last "string of chars") => "string of chars"
Congratulations!
This regression was introduced in Emacs 24.5.
Prior to that it would raise an error.
In 24.5 `safe-length' was substituted for
`length', and that results in what you see.
(`C-h f safe-length'.)
IMO this should be fixed. Someone(TM) will
maybe say it's intentional (but then it
should be doc'd).
`safe-length' is good, in avoiding pbs with
circular lists etc. But either some test
for a list (or a sequence) should be used
or this nontraditional behavior for `last'
should be doc'd. (`last' should of course
work with dotted lists.)
`last' is one of the oldest Lisp functions.
We really shouldn't be fiddling with its
behavior in unexpected ways.