Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: •Re: Why get() and set() accessor methods are evil

by Elian (Parson)
on Nov 25, 2003 at 21:46 UTC ( #310070=note: print w/replies, xml ) Need Help??


in reply to •Re: Why get() and set() accessor methods are evil
in thread Why get() and set() accessor methods are evil

Given that Larry's mandated that the Perl 6 compiler be capable of autogenerating lvalue accessor methods for attributes, I don't think you'll find that perl 6 will magically make things any better--you'll just end up with a lot of public attribute accessors.

Granted, a.b = 12; is much nicer looking than a.set_b(12); or a.set_b = 12

Replies are listed 'Best First'.
Re: Re: •Re: Why get() and set() accessor methods are evil
by Juerd (Abbot) on Nov 26, 2003 at 22:44 UTC

    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:

    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/;
    Having these things1 built-in in Perl 6 will make me happy.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

    1 The things that are called 'properties' in Perl 5 jargon2, but 'attributes' in Perl 6 jargon. Perl 5's 'attributes' are 'traits' in Perl 6. (IIRC)
    2 But not officially. They're often called 'attributes', but we should discourage that, because 'attributes' are already something else. Attributes are relatively new, though, so we can't blame writers of older documentation.
Re: Re: •Re: Why get() and set() accessor methods are evil
by sth (Priest) on Nov 26, 2003 at 19:00 UTC

    ...but not as nice as a->set_b(12). :-)
    ...I know, I know arrow notaion is going away in Perl6. :-(

      You seem disappointed that the arrow notation is going away. Personally, I think this is a Good Thing. Aside from the fact that it will mean less typing for common things, it's a holdover from the old days of C. In C, a struct's members could be accessed with dot notation:

      typedef struct { double x double y; } POINT; POINT some_point; /* and later in the code */ some_point.x = 1.02;

      However, if you later had passed a pointer to a struct, you would access the struct members with arrow notation:

        some_point->x = 1.02

      Since Perl was largely designed to be familiar to C programmers, having an arrow to dereference things seemed fine. However, Perl 6 seemingly is intended to be familiar to a larger audience (in some ways), it makes sense to drop the C style arrow notation in favor of the dot notation that popular languages use.

      And just to keep things topical, by using the arrow notation, we may be encouraging C programmers to use objects like structs and that's just silly. Which is kind of the point of much of this thread :)

      Cheers,
      Ovid

      New address of my CGI Course.

        Perl 6 seemingly is intended to be familiar to a larger audience (in some ways)

        Agreed, but I will still miss them. I prefer the -> over the . notation in C as well. Much of the C code I work with pass around lots of structures, between that and hash refs I am so used to typing -> I don't mind it. I understand why they are changing, it will still be a hard habbit to break. :-)

      Nah. a[insert method call punctuation here]set_b(foo) is just nasty. 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.

      One of the few places (well, OK, the only place so far, but I won't rule out others existing) that I like lvalue methods or subs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://310070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-10-23 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?