develooper Front page | perl.perl6.language | Postings from July 2005

Do I need "has $.foo;" for accessor-only virtual attributes?

Thread Next
From:
Sam Vilain
Date:
July 17, 2005 20:34
Subject:
Do I need "has $.foo;" for accessor-only virtual attributes?
Message ID:
42DB234C.6050106@vilain.net
Say I make an "accessor" method for an attribute that doesn't really
'exist'.

For instance, a good example of this is the "month_0" vs "month"
properties on a date object; I want to make both look equivalent as
real properties, but without the users of the class knowing which
one is the "real" one.

Users of the class includes people subclassing the class, so to them
they need to be able to use $.month_0 and $.month, even though there
is no "has $.month_0" declared in the Class implementation, only
"has $.month".

So, is $.month just shorthand for $?SELF.month, which happens to be
optimised away to a variable access in the common case where the
"method month" isn't defined, or has a sufficiently simple "is
accessor" trait in its declaration?

And that, in turn, $:month is actually $?SELF.(":month"), where
":month" is an alias for the submethod called "month".  After all, we
want Roles used by Classes to have access to this virtual attribute
coolness, too.

These simple definitions should make all sorts of OO tricks possible,
and reduces the definition of Classes to one that is purely
functional (ie, state is just a set of functions too).

Sam.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About