unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Przemysław Kryger" <pkryger@gmail.com>
To: emacs-devel@gnu.org
Subject: Should copy-tree assume bool vectors are vectors?
Date: Sat, 30 Sep 2023 14:32:56 +0100	[thread overview]
Message-ID: <5A6E2AF9-450A-4051-B4ED-972213855101@gmail.com> (raw)

Hello,

Recently I’ve stumbled on a somewhat surprising behaviour of `copy-tree’
function. Well it was surprising to me at least. The behaviour is that bool
vectors are not copied when VECP argument of `copy-tree’ is non-nil. For
example, my expectation was the following code to not assert:

  (let* ((vec (make-vector 1 nil))
         (bvec (make-bool-vector 1 nil))
         (tree (cons vec bvec))
         (copy (copy-tree tree t)))
    (cl-assert (equal tree copy))
    (aset vec 0 t)
    (aset bvec 0 t)
    (cl-assert (not (equal tree copy)))
    (cl-assert (equal (car tree) vec))
    (cl-assert (not (equal (car copy) vec)))
    (cl-assert (equal (cdr tree) bvec))
    (cl-assert (not (equal (cdr copy) bvec))))

I made that assumption after reading the documentation of `copy-tree’:

> […] With second argument VECP, this copies vectors as well as conses.

and info node of `copy-tree’: 5.4 Building Cons Cells and Lists

> […] However, if VECP is non-‘nil’, it copies vectors too (and operates
  recursively on their elements).

and info node: 6.7 Bool-vectors:

> A bool-vector is much like a vector, except that it stores only the values
  ‘t’ and ‘nil’.

My understanding is that the existing behaviour stems from the fact that
`vectorp’, that `copy-tree’ uses, yields nil for bool vectors. 

I can see at least the following options, but perhaps even better solution can
be found:

1. Do nothing. After all bool vectors are not vectors - `vectorp’ clearly
returns nil, and there is a separate function `bool-vector-p` for the latter.

2. Update documentation to explicitly point out above discrepancy, including
some (all?) aforementioned places and perhaps `vectorp’ and any other relevant
places.

3. Update the code of `copy-tree’ to support copying bool vectors when VECP is
non-nil.

I’m happy to contribute patches, should a need arise to modify anything.
Cheers,
PK




                 reply	other threads:[~2023-09-30 13:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5A6E2AF9-450A-4051-B4ED-972213855101@gmail.com \
    --to=pkryger@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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