in reply to Re: •Re: Why get() and set() accessor methods are evil
in thread Why get() and set() accessor methods are evil
Granted, a.b = 12; is much nicer looking than a.set_b(12); or a.set_b = 12
(Other post: If it's supposed to be an assignment it should look like an assignment, and setting a property or attribute is an assignment no matter how it's mediated under the hood.)
FYI, in case you didn't know about it already, Attribute::Property makes a->b = 12 work in Perl 5. From its synopsis:
my $object = SomeClass->new(digits => '123'); $object->nondigits = "abc"; $object->digits = "123"; $object->anyvalue = "abc123\n"; $object->anyvalue('archaic style still works'); my $john = Person->new(name => 'John Doe', age => 19); $john->age++; printf "%s is now %d years old", $john->name, $john->age;
When modifying a value using an accessor method (which we dubbed 'archaic style'), things get ugly:
Having these things1 built-in in Perl 6 will make me happy.Old: $john->age($john->age + 1); $john->name->(do { (my $n = $john->name) =~ s/Doe/Smith/; $n }); New: $john->age++; $john->name =~ s/Doe/Smith/;
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
|
---|