Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by hardburn (Abbot)
on Nov 25, 2003 at 18:02 UTC ( [id://309987]=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

(Also this is not an OO issue, but merely a Perl issue.)

The example might have shown a problem specific to Perl, but the idea is applicable to any language. A Java API that returns an array of integers stored in an otherwise private field would be just as problematic.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: Why get() and set() accessor methods are evil
by pg (Canon) on Nov 25, 2003 at 18:07 UTC

    You are right, as Java also has primitive types.

    But on the other hand, in this case, I can choose to return, for example, an ArrayList of Integer. This is much less a problem to languages that have stronger OO support.

    Update:

    Thanks BUU for the extension, tie was actually something I thought of. The idea of tie, actually holds some characteristics that OO interface provides.

      So why couldn't/wouldn't you do the exact same in perl? You could even do it transparently via tie, or you could do it overtly by creating the object. Seems fairly simple to me at least.
      package ArrayList; my $i; sub new{shift;return bless{[@_]=>shift};} sub iterate{return $_[0]->[$i++]}
      And so on and so forth.

        One reason you wouldn't want to is that is Perl's context-sensitivity. By using ArrayList, which would be implemented as a scalar holding a referance, you're now putting things in scalar context that really should be in list context.

        BTW--Check out the Class::Iterator module for doing this.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-16 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found