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

Quick translation wanted

12 views
Skip to first unread message

Simon Cozens

unread,
Dec 6, 2002, 9:54:43 AM12/6/02
to perl6-l...@perl.org

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} }

--
"They laughed at Columbus, they laughed at Fulton, they laughed at the
Wright brothers. But they also laughed at Bozo the Clown."
-- Carl Sagan

Luke Palmer

unread,
Dec 6, 2002, 10:44:23 AM12/6/02
to si...@simon-cozens.org, perl6-l...@perl.org
> Mailing-List: contact perl6-lan...@perl.org; run by ezmlm
> Sender: si...@simoncozens-2.dsl.easynet.co.uk
> From: Simon Cozens <si...@simon-cozens.org>
> 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;

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

Larry Wall

unread,
Dec 6, 2002, 2:15:20 PM12/6/02
to perl6-l...@perl.org
On Fri, Dec 06, 2002 at 08:44:23AM -0700, Luke Palmer wrote:
: > Mailing-List: contact perl6-lan...@perl.org; run by ezmlm

: > Sender: si...@simoncozens-2.dsl.easynet.co.uk
: > From: Simon Cozens <si...@simon-cozens.org>
: > 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

Luke Palmer

unread,
Dec 6, 2002, 2:27:31 PM12/6/02
to la...@wall.org, perl6-l...@perl.org
> Date: Fri, 06 Dec 2002 11:15:20 -0800
> From: Larry Wall <la...@wall.org>

>
> As for constructor syntax, I suppose we might make use of the $. notation
> like this:
>
> method new($.name, $.age) {
> return $class.bless;
> }

Come to think of it, new is a class method, not an object method. How
is that specified again? Is there going to be sugar to blur that
distinction for constructors?

Luke

Larry Wall

unread,
Dec 6, 2002, 2:40:00 PM12/6/02
to perl6-l...@perl.org
On Fri, Dec 06, 2002 at 12:27:31PM -0700, Luke Palmer wrote:
: > Date: Fri, 06 Dec 2002 11:15:20 -0800

You'll not that $class was declared typeless, so it doesn't matter if it was
actually a class or an object of that class.

Actually, that last example was wrong--it should have been

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

or

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

But in either case, if $class is untyped, it can work with either cloning
or class-based constructors.

Larry

0 new messages