On Jul 24, 2022, at 19:04, Jean Abou Samra <[email protected]> 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