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

Re: default values for attributive parameters

Thread Previous
From:
Juerd
Date:
April 21, 2005 04:49
Subject:
Re: default values for attributive parameters
Message ID:
20050421114910.GJ19716@c4.convolution.nl
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

Thread Previous


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