Message from discussion
A12: default accessors and encapsulation
Newsgroups: perl.perl6.language
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.perl.org
Return-Path: <la...@london.wall.org>
Mailing-List: contact perl6-language-h...@perl.org; run by ezmlm
Delivered-To: mailing list perl6-langu...@perl.org
Received: (qmail 5043 invoked from network); 23 Apr 2004 20:27:59 -0000
Received: from x1.develooper.com (63.251.223.170)
by onion.develooper.com with SMTP; 23 Apr 2004 20:27:59 -0000
Received: (qmail 24418 invoked by uid 225); 23 Apr 2004 20:27:58 -0000
Delivered-To: perl6-langu...@perl.org
Received: (qmail 24413 invoked by alias); 23 Apr 2004 20:27:58 -0000
X-Spam-Status: No, hits=2.6 required=7.0
tests=RCVD_IN_DYNABLOCK,RCVD_IN_SORBS
X-Spam-Check-By: la.mx.develooper.com
Received: from pimout2-ext.prodigy.net (HELO pimout2-ext.prodigy.net) (207.115.63.101)
by la.mx.develooper.com (qpsmtpd/0.27.1) with ESMTP; Fri, 23 Apr 2004 13:27:56 -0700
Received: from wall.org (adsl-64-172-47-53.dsl.snfc21.pacbell.net [64.172.47.53])
by pimout2-ext.prodigy.net (8.12.10/8.12.10) with ESMTP id i3NKRr2G046538
for <perl6-langu...@perl.org>; Fri, 23 Apr 2004 16:27:54 -0400
Received: from london.wall.org (localhost.localdomain [127.0.0.1])
by wall.org (8.12.10/8.12.10) with ESMTP id i3NKRrbf012338
for <perl6-langu...@perl.org>; Fri, 23 Apr 2004 13:27:53 -0700
Received: (from larry@localhost)
by london.wall.org (8.12.10/8.12.10/Submit) id i3NKRrHS012336
for perl6-langu...@perl.org; Fri, 23 Apr 2004 13:27:53 -0700
Date: Fri, 23 Apr 2004 13:27:53 -0700
To: Perl 6 Language <perl6-langu...@perl.org>
Subject: Re: A12: default accessors and encapsulation
Message-ID: <20040423202753.GB11876@wall.org>
Mail-Followup-To: Perl 6 Language <perl6-langu...@perl.org>
References: <1082747529.2893.333.camel@pps> <ICELKKFHGNOHCNCCCBKFGEPICLAA.Austin_Hastings@Yahoo.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <ICELKKFHGNOHCNCCCBKFGEPICLAA.Austin_Hastings@Yahoo.com>
User-Agent: Mutt/1.4.1i
X-Spam-Rating: onion.develooper.com 1.6.2 0/1000/N
Approved: n...@nntp.perl.org
From: la...@wall.org (Larry Wall)
On Fri, Apr 23, 2004 at 03:23:09PM -0400, Austin Hastings wrote:
: > And if you override the accessor, you can:
: >
: > multi method foo(Str $blah = undef) is rw($new) {
: > (my($old),$.foo)=($.foo,$blah//$new);
: > .update_the_world_in_some_cool_way();
: > return $old
: > }
:
: I don't understand this.
It's what we on the cabal have been calling an out-of-band parameter.
We have at times contemplated them for various purposes, none of
which seem to have panned out. Instead we've ended up with things
like $CALLER::_ and such.
: What's the $new doing?
It looks to me like it's being a parameter that doesn't interfere
with the official signature.
: And if you've only got one of them, do you need multi, or just an optional
: argument?
It's just an optional argument, but...
: method foo(Str ?$new) is rw {
: my $old = $.foo;
: if (defined($new)) {
: $.foo = $new;
: .update_the_world();
: }
: return $old;
: }
...but it doesn't ruin the signature symmetry between getter and
setter like yours. It does share the misfeature that you have to
write special switching code internally to deal with it, which the
proxy solution automatically dispatches for you. It also assumes
you even want the old value, which you often don't, if the proxy is
destined to be the left side of an ordinary assignment in void context.
And, in fact, if it weren't in void context, you'd want the new value,
not the old one, which means you better hang onto that proxy...
I suppose in the cases where the attribute can serve as its own proxy,
we could have a hook that captures control on setting, and lets you
add side effects en passant without making you responsible to actually
set the variable like a STORE hook would. If you wanted to remove the
actual attribute you'd have to graduate it to a STORE closure on a
real proxy though (with whatever syntactic sugar makes that most
palatable).
Larry