Dave Whipp wrote:
The sequence I was thing of is:

my $obj = Length.new;
$obj.mm = 42;
say $obj.inch; # your "Length.inch" method yields
$obj.mm = 63;
say $obj.inch; # Length.inch to resumes, overwriting $obj.mm back to 42

It seems to me that the assignment within your "inch" method will occur *after* my assignment of "$obj.mm = 63"

You have two invocations of the .inch method. Each returns a different
ResumeProxy instance. Both are never resumed because they aren't even
referenced. You need to say something like '$i1 = $obj.inch' to read
the value of the object and to keep the ResumeProxy. That of course
means you can write

   my $obj = Length.new;
   $obj.mm = 42;
   my $inch1 = $obj.inch;
   $obj.mm = 63;
   my $inch2 = $obj.inch;
   my $inch3 = $obj.mm * 25.4; # outer conversion

   $inch1 = 3; # resumes first invocation, which overwrites
               # $.mm == 63

   say $obj.mm; # prints 3 * 25.4 (note that I messed that up)

   $inch2 = 2; # resumes second invocation, which overwrites
               # $.mm == 3 * 25.4 and stores that value in $inch2

   say $inch2; # surprisingly prints 3 * 25.4 != 2

   $inch3 = 7; # resumes nothing because dispatch goes to
               # infix:<=>:(Num,Int) or so.

The really important distinction that is far from obvious is the
one between assignment to $inch2 and $inch3! The point is that
the assignment is dispatched on the *value* of the lhs and stored
in the container holding it---checking the container's constraint
while doing so. If you don't want that, the implementation needs
to be changed to read

   multi sub *infix:<=>(ResumeProxy $lhs is rw, $rhs)
      $lhs.resume($rhs); # finish side effects
      $lhs = $rhs; # default store operation on container

But that gets us to the thread 'parameters: ref vs rw' when you

    $obj.inch = 3; # autovivifies anonymous container?

This anonymous container is needed in the second line of infix:<=>
from above. An extreme case of this is to dispatch '3 = 4' to
infix:<=>:(Int is rw, Int) with an anonymous container for 3. Thus
you get the side-effects and no error!

Regards, TSa.

"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan

Reply via email to