Raku / problem-solving

🦋 Problem Solving, a repo for handling problems that require review, deliberation and possibly debate
Artistic License 2.0
70 stars 16 forks source link

Are `Captures` value types or not? #372

Open lizmat opened 1 year ago

lizmat commented 1 year ago

Capture instances are currently implemented as being a value type (basically since https://github.com/rakudo/rakudo/commit/c855a0f439 ).

However, since https://github.com/Raku/old-issue-tracker/issues/4360 it has made elements of it mutable (if they were created with containers).

If an element in a Capture is changed, it's associated .WHICH will also change. This violates the principle of immutability of value types.

Making Capture instances immutable, breaks spectests. Making the Capture class an object type, also breaks spectests.

lizmat commented 1 year ago

Propose to:

Ideally to be done with a language level check, but I think that will turn out to be pretty hard to do.

jnthn commented 1 year ago

The primary purpose of Capture in the language is to model an argument list, and arguments may be mutated by is rw parameters. For example, consider:

sub foo($p is rw) {
    $p++;
}

my $x = 1;
my \c = \($x);
foo(|c);
say c;
lizmat commented 1 year ago

@jnthn So then it shouldn't be a value type then?

jnthn commented 1 year ago

Correct, although it'd make sense for snapshot equality (eqv) to work.