On Fri, Dec 06, 2002 at 08:44:23AM -0700, Luke Palmer wrote:
: > Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
: > Sender: [EMAIL PROTECTED]
: > From: Simon Cozens <[EMAIL PROTECTED]>
: > Date: 06 Dec 2002 14:54:43 +0000
: > Organization: Bethnal Green is PEOPLE!
: > X-Posted-By: 217.204.174.162
: > 
: > 
: > Is it clear how attributes accessors on objects are going to work yet?
: > I need to say something along the lines of:
: > 
: >    sub new {
: >    my $class = shift;
: >    my ($name, $age) = @_;
: >       bless {
: >          name => $name,
: >          age  => $age
: >       }, $class;
: >    }
: >   
: >    sub age { my $self=shift; $self->{age} }
: > 
: 
: There's nothing decided on how constructors work yet, so I'll use a
: fill-in syntax.
: 
: class Person {
:     has $.name is private;    # No accessor generated
:     has $.age;

Attributes are probably private by default.  Methods are public
by default.

:     method new($n, $a) {
:         ($.name, $.age) = ($n, $a);
:         return bless;         # bless me => myself
:     }
: }

That would need to be a .bless, since bless is not longer a
builtin, but a class method.  So I'd make it:

    class Person {
        has $.name;
        has $.age is public;

        method new($n, $a) {
            ($.name, $.age) = ($n, $a);
            return .bless;
        }
    }

which is short for something like:

    class Person {
        has $.name;
        has $.age;

        method new($class: $n, $a) {
            ($.name, $.age) = ($n, $a);
            return $class.bless;
        }

        method age () is rw {
            return $.age;
        }
    }

There may well be a property on a class that makes all its attributes
public by default, so it acts more like a struct in C++.

As for constructor syntax, I suppose we might make use of the $. notation
like this:

    method new($.name, $.age) {
        return $class.bless;
    }

and it would be assumed that those args go straight into the object.  We
also have to thrash out the difference between "new" and "init".  An
init() call might occur within a bless, in which case you might just
write

    method init($.name, $.age) {...}

Maybe the default initializer is

    method init($.name = undef, $.age = undef, *@extras) {...}

So we might actually end up with a constructor calls looking like

    $class.bless(name => $me, rank => "low", serial => 12345);

Named parameters tend to work a lot better than positional for constructors,
especially if the parameters intended for a different initializer can be
ignored.

But this is all Apo 12, so still highly speculative...

Larry

Reply via email to