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

Reply via email to