in reply to Perl OO and accessors
Presumably you'd also want
get_radius() get_area()
but then you could use the magic of lvalue subs and tie to let you replace these 4 functions with 2 accessors (it's much less messy in ruby or python)
radius area
so you could do
$c->area = $c->area / 2; $c->radius = $c->radius * 2;
Neither of these expose an attribute, despite how it looks. Whether the inplementation actually stores the radius or the area it doesn't matter, neither of these are exposed here, because all interaction is mediated by the methods.
The only thing that is "evil" is unmediated (actually unmediatable) exposure of an attribute. In Java, where this notion of "evil" originates, there is no such thing as mediated exposure of an attribute. The evil in Java is that once an attribute is exposed, it cannot be unexposed and so the interface dictates the implementation forever more.
In languages like Perl, Python, Ruby, Object Pascal and many others it is possible to create something that looks like an attribute but does not force anything on the implementation and so there is no evil. In some of these you can even expose an unmediated attribute and later turn it into a mediated attibute without changing the interface (hence the "unmediatable" above).
So in some languages exposure is evil because the exposure is real and irrevocable but other languages allow you apparently expose or to revocably expose an attribute without forcing anything on the implementation.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl OO and accessors
by sauoq (Abbot) on Nov 29, 2005 at 04:07 UTC | |
by Zaxo (Archbishop) on Nov 29, 2005 at 08:47 UTC | |
by Aristotle (Chancellor) on Nov 29, 2005 at 04:16 UTC | |
by sauoq (Abbot) on Nov 29, 2005 at 04:32 UTC | |
by Aristotle (Chancellor) on Nov 29, 2005 at 04:36 UTC | |
by sauoq (Abbot) on Nov 29, 2005 at 04:48 UTC | |
| |
by sauoq (Abbot) on Nov 29, 2005 at 04:43 UTC | |
by fergal (Chaplain) on Nov 29, 2005 at 11:09 UTC |