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

default values for attributive parameters

4 views
Skip to first unread message

Carl Franks

unread,
Apr 21, 2005, 6:29:07 AM4/21/05
to perl6-language
Are default values supported for attributive parameters in an argument list?

I wish to convert these 2 subroutines to perl6:

sub foo {
my $self = shift;

$self->{foo} = defined $_[0] ? shift : undef;
}

sub bar {
my $self = shift;

$self->{bar} = defined $_[0] ? shift : $DEFAULT;
}

Is this correct?

method foo (?$.foo = undef) {};

method bar (?$.bar = $DEFAULT) {};


Thanks,
Carl

Juerd

unread,
Apr 21, 2005, 7:49:10 AM4/21/05
to ca...@fireartist.com, perl6-language
Carl Franks skribis 2005-04-21 11:29 (+0100):

> I wish to convert these 2 subroutines to perl6:
> sub foo {
> my $self = shift;
> $self->{foo} = defined $_[0] ? shift : undef;
> }
> sub bar {
> my $self = shift;
> $self->{bar} = defined $_[0] ? shift : $DEFAULT;
> }
> Is this correct?

Those are weird methods. They're essentially write-only accessor methods
for their respective internal attributes (in perl 5 called properties).

> method foo (?$.foo = undef) {};
> method bar (?$.bar = $DEFAULT) {};

The fun thing is that

has $.foo;
has $.bar;

already gives you, for free and imlicitly, two lvalue accessor methods
that can be used for both reading and writing the value. You appear to
want a default that is used at set-time, which I find weird. More common
I think would be to provide a default that is used at the time of object
construction.

has $.foo;
has $.bar = $DEFAULT;


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

0 new messages