Subject: Re: bug#75170: add-to-alist: new function

From: Robert Pluim <[email-masked]>
Date: Mon, 20 Jan 2025 12:27:34 +0100

>>>>> On Sat, 18 Jan 2025 11:33:28 +0200, Eli Zaretskii <[email-masked]> said:

    Eli> Ping!
    >> Cc: [email-masked]
    >> Date: Sun, 05 Jan 2025 17:21:00 +0200
    >> From: Eli Zaretskii <[email-masked]>
    >> 
    >> > From: Roland Winkler <[email-masked]>
    >> > Cc: [email-masked]
    >> > Date: Sun, 29 Dec 2024 08:50:18 -0600
    >> > 
    >> > On Sun, Dec 29 2024, Eli Zaretskii wrote:
    >> > > What is the advantage of adding this function, given that add-to-list
    >> > > can be used with alists, and given that alist-get can nowadays be used
    >> > > as a generalize variable?
    >> > 
    >> > The advantage I see for also having the function add-to-alist is the
    >> > following:
    >> > 
    >> > add-to-list checks for the presence of an element in a list.  In the
    >> > case of alists, this means it checks for the presence of associations.
    >> > You cannot easily modify an existing association with add-to-list.  If
    >> > you have an alist with association (foo . bar) and you call add-to-list
    >> > with an element (foo . baz), add-to-list will not remove the association
    >> > (foo . bar), but the alist will then contain both associations.
    >> > 
    >> > add-to-alist checks for the presence of keys and it makes sure that each
    >> > key appears only once in an alist.  By default, it replaces the value of
    >> > an existing key.  This makes it easy to modify an existing association.
    >> > Only with the optional arg NO-REPLACE non-nil, it will preserve an
    >> > existing association.
    >> > 
    >> > Say, I want in my .emacs file a more complicated association for a key,
    >> > and I do not get initially what I want.  I can call add-to-alist
    >> > multiple times, till I get what I want.
    >> > 
    >> > Is there a simple way to accomplish this in other ways (a way that we
    >> > recommend for users in their init file if they do not want to use
    >> > customize like me)?
    >> > 
    >> > Would it make sense to give this functions a different name if more
    >> > often it may be used to modify existing associations in an alist instead
    >> > of adding new ones?
    >> 
    >> Let's hear the other co-maintainers.
    >> 
    >> Stefan and Andrea, WDYT about this?  Should we add this function?

Iʼm not sure what this would offer over

    (setf (alist-get key alist) value)

which will add a key->value mapping if it doesnʼt exist, and replace
it if it does. I also donʼt see a real use for retaining the existing
mapping: `alist-get' will return the first one anyway.

Robert
--