On Sat, 5 Oct 2002, John Williams wrote:
: I think everyone agrees that some sort of simple accessor syntax will be 
: included (instead of the getX/setX hack).  But will accessors _look_ like 
: attributes or methods?
: 
:   # look like methods
:       object.foo($value);
: 
:   # look like attributes
:       object.foo = $value;
: 
: Personally, I hope they look like attributes.

They will, outside the class anyway.  Inside it's $.foo.

: But if they do, the perl5 
: lvalue subs are not the way to do it.  Why?  Because an lvalue sub returns 
: a lvalue which get set _after_ the sub returns.  At that point it is too 
: late for the sub to do anything useful with the new value.

Lvalue methods will have some kind of optional property which specifies
a closure to execute after the modification happens.

    method foo {
        my $handle = find_database("random criteria");
        return $handle.fetch();

        WRITE {
            $handle.store(shift);
        }
    }

Or some such.  Note how the internal block allows capture of the $handle from
the original closure.   (A READ closure was also discussed in Zurich.)

Potentially there's also an "also" approach, which adds a closure to
an implicit autogenerated method:


    has $.foo is public;

    also &foo is write { write_database($_) };

Except that $_ (the first arg) should maybe be the object, not the
new value.  How do we name the parameter?  Hmm...

    also &foo is write ->:$newval { db_store($newval); .written(1); };

Yow.

Larry

Reply via email to