I am also a fan of chaining, but I think the average user would intuitively expect the accessor to return the new value of the attribute. Particularly when using modifiers, type constraints, or coercion (or any other features that make Moose so useful), I would not assume:
$val = "may be coerced/rejected/etc"; $obj->myattr($val); die unless $val == $obj->myattr(); Though, it would still be convenient if one had the option for the chaining semantics. But I would hope it would have to be an explicit choice. Chad On Thu, Mar 12, 2009 at 00:27, Derek Price <de...@ximbiot.com> wrote: > Actually, I'd be as happy with chained setters - it just bothers me to > see a useless return value. I'll keep both ideas in mind, thanks! > > Derek > -- > Derek R. Price > Solutions Architect > Ximbiot, LLC <http://ximbiot.com> > Get CVS and Subversion Support from Ximbiot! > > v: +1 248.835.1260 > f: +1 248.835.1263 > > > Sartak wrote: > > Hi Derek, > > > > On Tue, Mar 10, 2009 at 9:48 PM, Derek Price <de...@ximbiot.com> wrote: > >> Just a quick thought that occurred to me as I was reading > >> Moose::Unsweetened... I hope the API isn't set in stone yet... > > > > What getters and setters return certainly is set in stone. We'd break > > to many apps if we changed it three years in. > > > > But that's okay.. > > > >> If the > >> accessors presented there are correct, then the accessor functionality > >> might be slightly more efficient from a caller's perspective if the > >> "old" (also "current", with no arg) value was always returned. Then, > >> something like the following could be useful: > >> > >> my $old = $user->email_address($new); > >> > >> Whereas now, the above call would return $new, which is already known by > >> the caller and therefore not useful. > >> > >> The new accessor would look something like this: > >> > >> sub email_address { > >> my $self = shift; > >> my $v = $self->{email_address}; > >> > >> if (@_) { > >> $self->_validate_email_address( $_[0] ); > >> $self->{email_address} = $_[0]; > >> } > >> > >> return $v; > >> } > >> > >> Regards, > >> > >> Derek > > > > Moose is quite extensible. If you're adventurous enough, you could > > make this work as an extension to Moose. I don't know if we have for > > handy examples of modifying accessors, even in the MooseX ecosystem. > > > > I do know that a few people really want "chained" setters, which > > return the object, so that you could do: > > > > $datetime->set_hour(0)->set_minute(0)->set_second(0); > > > > Your proposed extension would have a very similar structure, being > > different in only a few lines. We'd be happy to help you write it! > > > > If you want to start poking, check out the following documentation: > > > > Moose::Manual::MOP > > Moose::Manual::MooseX > > > > As well as the "meta" and "extending" chapters of the Cookbook. > > > > Shawn >