Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

object property syntax

20 views
Skip to first unread message

Todd W.

unread,
Sep 24, 2003, 7:53:39 PM9/24/03
to perl6-l...@perl.org
I have a question/request concerning perl6 object properties.

I've done some work with .NET and They have come up with a really slick way
to handle object properties.

A typical property definition in VB.NET looks like:

Public Property description() As String
Get
return aString
End Get
Set(ByVal Value As String)
' set value
End Set
End Property

You can use it like this:

oObj.description = sDescr ' calls setter
sDescr = oObj.description ' calls getter

This allows proper encapsulation of object properties while providing a very
clean interface. A corollary feature is that properties can map to arbitrary
values. I've written object property definitions that read and write
directly to relational databases, and Ive written read only properties that
abstract XPath queries to config files.

My main interest in the feature is using the assignment operator to assign
to object properties.

I posted a question to CLPM on how to do this with perl5 and we decided to
use an 'lvalue' attribute on the subroutine and then make the returned
lvalue in the sub a tied variable to intercept read/writes:

http://groups.google.com/groups?th=5489106ee6715c8e

It works, but its obviously slow and can get confusing for anything
moderately complex.

I just thought I would share this and see if anything similar can fit in the
perl6 OO syntax.

Todd W.


Todd R Wade

unread,
Sep 25, 2003, 2:14:22 AM9/25/03
to perl6-l...@perl.org
I have a question/request concerning perl6 object properties.

I've done some work with .NET and They have come up with a really slick
way to handle object properties.

A typical property definition in VB.NET looks like:

Public Property propertyName() As String

Luke Palmer

unread,
Sep 25, 2003, 4:58:33 AM9/25/03
to Todd W., perl6-l...@perl.org
Todd W. writes:
> I have a question/request concerning perl6 object properties.

Rather, attributes. Properties are out-of-band data attached to a
particular object.

> I've done some work with .NET and They have come up with a really slick way
> to handle object properties.
>
> A typical property definition in VB.NET looks like:
>
> Public Property description() As String
> Get
> return aString
> End Get
> Set(ByVal Value As String)
> ' set value
> End Set
> End Property
>
> You can use it like this:
>
> oObj.description = sDescr ' calls setter
> sDescr = oObj.description ' calls getter
>
> This allows proper encapsulation of object properties while providing a very
> clean interface. A corollary feature is that properties can map to arbitrary
> values. I've written object property definitions that read and write
> directly to relational databases, and Ive written read only properties that
> abstract XPath queries to config files.
>
> My main interest in the feature is using the assignment operator to assign
> to object properties.

Okay, this has been thought through. It's the way lvalue subs are going
to be pulled off in the more complex cases.

I can't remember the exact syntax, but it goes something like:

sub assign_to_me() is rw {
my $ret is class { STORE { print "I was assigned to!\n" } }
}

(Note that ret is both being created and returned in the same
expression)

Admittedly, that's a bit of a kludge, so there may be some syntax to
sugar it up. I'd suggest an omitted C<do> property, with two additional
properties: C<fetch> and C<store>.

sub assign_to_me() is rw
will fetch { print "I was read from!\n" }
will store { print "I was assigned to!\n" }
{...}

Not to worry, as this is a nice thing to be able to do, it will be easy.

Luke

Matthijs Van Duin

unread,
Sep 25, 2003, 5:41:08 AM9/25/03
to Todd W., perl6-l...@perl.org
On Wed, Sep 24, 2003 at 07:53:39PM -0400, Todd W. wrote:
>I posted a question to CLPM on how to do this with perl5 and we decided to
>use an 'lvalue' attribute on the subroutine and then make the returned
>lvalue in the sub a tied variable to intercept read/writes:
>
>http://groups.google.com/groups?th=5489106ee6715c8e

Note that this has already kinda been done:

http://search.cpan.org/~juerd/Attribute-Property-1.04/Property.pm

This only does setter-code (the getter always uses an element of the object)
but it's perhaps interesting to look at.

>It works, but its obviously slow

A::P uses the 'Want' module (if installed) to speed up the common cases.

--
Matthijs van Duin -- May the Forth be with you!

Dan Sugalski

unread,
Sep 25, 2003, 9:58:33 AM9/25/03
to Todd W., perl6-l...@perl.org
On Wed, 24 Sep 2003, Todd W. wrote:

> I have a question/request concerning perl6 object properties.
>
> I've done some work with .NET and They have come up with a really slick way
> to handle object properties.
>
> A typical property definition in VB.NET looks like:
>
> Public Property description() As String
> Get
> return aString
> End Get
> Set(ByVal Value As String)
> ' set value
> End Set
> End Property
>
> You can use it like this:
>
> oObj.description = sDescr ' calls setter
> sDescr = oObj.description ' calls getter

Perl 6 will do something similar. Attributes (aka slot variables, aka
properties, aka "those thingies in the object") will, if you tell perl,
automatically get an lvalue method of the same name created for them. So
if you have a class that looks like (Excuse the syntax, I'm an internals
guy):

class Foo {
my $.attr1 is rw;
}

and an object of class Foo:

my Foo $bar;

then you can read and write that attribute with code like:

$bar.attr1 = "fooey";
print $bar.attr1, " to you!";

This has the nice property of allowing you to override the access to the
attribute if you want by defining an lvalue method with the same name as
the attribute, in which case that method will be called instead.

Dan

Dan Sugalski

unread,
Sep 25, 2003, 9:53:59 AM9/25/03
to Luke Palmer, Todd W., perl6-l...@perl.org
On Thu, 25 Sep 2003, Luke Palmer wrote:

> Todd W. writes:
> > I have a question/request concerning perl6 object properties.
>
> Rather, attributes. Properties are out-of-band data attached to a
> particular object.

FWIW, "attribute" and "property" are two words that have a meaning that
shifts depending on what language you're talking about. IIRC, .NET
languages have them exactly reversed from perl 6's usage...

Dan

0 new messages