Re: RFC 171 (v1) my Dog $spot should call a constructor implicitly

[email protected] (Piers Cawley)
Newsgroups perl.perl6.language.objects
Message-ID <[email protected]>
Nathan Wiger <[email protected]> writes:

> Piers Cawley wrote:
> > 
> > Eeeewwwwww. Most of the time I use 'my Dog $spot' is along the lines
> > of:
> > 
> >     my Dog $spot = Dog::Dalmation->new(name => 'Spot');
> >     $spot->bark;
> 
> No problem, that still works, according to the RFC...

It does? Note that, whilst @Dog::Dalmation::ISA may well contain the
string 'Dog', it's highly unlikely that 

    my Dog $spot;

Would know that I actually want a new Dalmation.

Also, you've managed to miss the more important case where I want to
use a factory method:

    my Dog $patches = $dog_pound->get_cute_stray;

Where all I know is that the factory method is going to return a Dog
of some sort.

Or what about:

    print "What sort of dog do you want? ";
    my $dog_type = ucfirst(<STDIN>); chomp $dog_type;

    my Dog $puppy = "Dog::${dog_type}"->new(name => $dog_name);

In *none* of these cases can you guarantee that 'ref $foo' will be
'Dog', but you are instead asserting that '$foo->isa("Dog")'.
    

> 
> >    sub bark {
> >        my Dog $self = shift;
> >        print $self->name, " barks!\n";
> >    }
> 
> Here I guess I miss the point of the Dog inside the bark() sub? It seems
> unnecessary to me. Maybe a better example of what you're trying to get
> at?

Because, hopefully, there are efficiency gains to be had when perl
*knows* that $self is a Dog. If it only makes the 'name' method call
work faster then it's a good result. Admittedly 'bark' is a fairly
simple function method with only one other method call, but in more
complex methods one would expect efficiency gains.


> > There's a whole host of occasions when you want 'my Dog $spot' but you
> > *don't* want $spot to be set to Dog->new.
> 
> Well, I think the actual method name would be CREATE. So you could have
> either a new(), or a CREATE(), or both. Use whichever one you want, no
> sweat.
> 
> The default core CREATE would simply instantiate a variable, which would
> then get set via Dog::Dalmation->new.

But how does it *know* it wants Dog::Dalmation->new when it's only
declared to be a Dog? Does the word 'Polymorphism' ring any bells with
you?

-- 
Piers
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.