develooper Front page | perl.perl6.language | Postings from August 2001

Re: CLOS multiple dispatch

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
August 29, 2001 23:02
Subject:
Re: CLOS multiple dispatch
Message ID:
20010830020226.C17751@blackrider
On Wed, Aug 29, 2001 at 10:49:25PM -0700, Hong Zhang wrote:
> What is the need for CLOS? Are we trying to build a kitchen
> sink here?

Umm, I think you mean "What's the need for multiple dispatch?"

Currently, if I want to write a method that does this:

    my $name = $foo->name;
    $foo->name($name);

I have to write:

    sub name {
        my($self) = shift;

        if( @_ ) {
            my $name = shift;
            $self->{name} = $name;
        }

        return $self->{name};
    }

With multiple dispatch, I could instead write:

    # Following RFC 256
    sub name (Foo $self) : multi {
        return $self->{name};
    }

    sub name (Foo $self, STRING $name) : multi {
        $self->{name} = $name;
        return $self->{name};
    }

which is quite a bit simpler.
        

-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
My lips, your breast and a whole lotta strange arguing.

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