[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: as_ly_scm_list
From: |
Dan Eble |
Subject: |
Re: as_ly_scm_list |
Date: |
Mon, 25 Jul 2022 07:38:23 -0400 |
On Jul 24, 2022, at 19:04, Jean Abou Samra <jean@abou-samra.fr> wrote:
>
> I understand that ly_scm_list takes an rvalue and disallows
> lvalues for clarity, while as_ly_scm_list takes an lvalue,
> and will fail on an rvalue by design, with the failure being
> a compilation failure since e67154f7e330b61c5d9a973fbb89bd56866a148e.
>
> But I don't understand the real difference that distinguishes
> them. Before that commit, what would have been the difference
> between as_ly_scm_list (lvalue) and ly_scm_list (lvalue)?
The difference is ownership of the head.
ly_scm_list has one data member:
private:
SCM head_ = SCM_EOL;
Creating an instance of ly_scm_list creates this head. If a list operation
modifies this head, no other SCM is modified.
ly_scm_list x (my_things_); // A. x.head_ = my_things_
...
x.clear (); // B. my_things_ is unchanged
(B) might or might not be a problem. Without a comment, it can require deep
review to tell whether leaving my_things_ alone was an error or not.
Forbidding (A) means that people can't write such code, but there are times
where we do want to work with a SCM like a ly_scm_list, including operations
that change the head SCM.
as_ly_scm_list casts a SCM to a ly_scm_list. There is no separate head because
there is no new ly_scm_list, just a reinterpretation of the given SCM.
auto& x = as_ly_scm_list (my_things_); // C.
…
x.clear (); // D. clears my_things_
Hope that helps.
—
Dan
- as_ly_scm_list, Jean Abou Samra, 2022/07/24
- Re: as_ly_scm_list,
Dan Eble <=