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

Re: construction clarification

Thread Previous | Thread Next
From:
Damian Conway
Date:
May 31, 2005 17:30
Subject:
Re: construction clarification
Message ID:
429D017C.5010705@conway.org
Carl Franks wrote:

> I have a class that normally takes a list of named arguments.
> I also want to be able to handle a single argument.
> 
> class Foo {
>   multi method new (Class $class: Str $date) {
>     return $class.bless(date => $date);
>   }
>   
>   submethod BUILD ($.date, $.time, $.offset) {
>     # some error checking here
>   }
> }
> 
> my $foo = Foo.new('2005-05-30');
> 
> #
> Is this the correct way to handle my scenario?

It's *a* correct way. But redundant in this particular case.
The universal new() would handle the one-argument call exactly the same
as your overloaded new() does. Presumably, however, the one-argument variant 
would do something else as well.


> My explicit 'new' method should only handle the case of 
> a single argument - otherwise the 'new' method 
> inherited from Class will handle a list of named 
> arguments.

Correct.

> My explicit BUILD submethod is called, regardless 
> of which 'new' method is called, correct?

Yes. It's invoked by bless().

Damian

Thread Previous | Thread Next


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