Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^5: It's a dog, but what kind? (polymorphism , in Perl OO) (!abstract)

by tye (Sage)
on Mar 23, 2004 at 23:06 UTC ( #339243=note: print w/replies, xml ) Need Help??

in reply to •Re: Re^2: It's a dog, but what kind? (polymorphism , in Perl OO)
in thread It's a dog, but what kind? (polymorphism , in Perl OO)

Yes, as I said, I understand the objection. I note that you don't quote what your alternative is nor acknowledge the problem that I was bringing up.

Did you give a concrete class when you asked your question? As I've said, I agree that there is a problem in theory, and that sometimes there is a problem in practice. But I've also found that, in practice, exactly what it means to get a 'new' object from an object of a *specific* class is often quite clear (and I don't think it usually means what you appear to be referring to as "clone" nor "copy").

You appear to be using "clone" to mean something close to "copy all of the object's attributes" and using "copy" (based on emphasis) to mean something close to "copy none of the object's attributes".

I don't think I've ever seen people talking about classes as having two types of attributes (that I'll define shortly). So I'm not surprised that asking questions in the abstract fail to get people to think about splitting attributes into two types. In practice, for many Perl classes, I think this split happens quite naturally.

I'll call the two types 'basis' attributes and 'convenience' attributes. The basis attributes are items that must be passed in to new(). The convenience attributes have more to do with the personal preferences of the user of the module.

$obj->new( $basis1, $basis2, ... ) creates a new object based on the passed-in basis attributes but copying the convenience attributes from $obj.

Now, some classes have attributes that don't clearly fall into one of these categories, and I suspect in that such cases the meaning of 'instance new' would not be as clear.

And I suspect that people who think $obj->new( ... ) should be "clone" are either thinking of convenience attributes or aren't thinking of the "..." part while people who think $obj->new( ... ) should do ref($obj)->new( ... ) aren't thinking of convenience attributes (that perhaps are more common in Perl OO than in other flavors of OO).

- tye        

Replies are listed 'Best First'.
Re^6: It's a dog, but what kind? (polymorphism , in Perl OO) (!abstract)
by hardburn (Abbot) on Mar 24, 2004 at 14:18 UTC

    The alternative is that if you want a copy or clone operation, they should be seperate methods from the constructor.

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      That is so far off from what I was talking about as far as what I meant by "alternative" and completely misses the point of why the obvious alternative is unacceptable to me that I suspect you did not bother to read Re^2: A few Perl OOP questions. (disparaging).

      Normally I'd try to steer back toward that but I'm just not coming up with anything. In fact, I'm not sure you even read and understood all of what I wrote in this thread.

      - tye        

        And tye, back at ya. Did you understand the responses you got to that posting, or the thread in this posting?

        The "alternative" is the simple approach:

        sub new { my $class = shift; my $self = { ... }; bless $self, $class; }
        None of the extra cut-n-pasted stuff to make $instance->new do anything in particular, because as I said in my quoted item above, it's wrong to two-thirds of the audience, and therefore is dangerous.

        If that's confusing to you, I wish you could say what you're missing in a different way, because I'm not getting it.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        Re^2: A few Perl OOP questions. (disparaging) shows what happens when the common Perl idiom $class = ref $class || $class isn't followed and someone expects it to be implemented that way. I would thus argue that someone expecting new to act like clone when it isn't explicitly documented is relying on implementation details of the class. Even if it is documented, it's still a bad idiom.

        If you want alternatives, here they are:

        package Foo; use base 'Clone::PP'; # And don't bother writing it yourself sub new { my $class = shift; my $self = { }; # Intitilze $self bless $self, $class; }

        Although people shouldn't be expecting the constructor to make a clone or copy anyway, this idiom is so common that it may be wise to explicitly document that your constructor won't work that way. Better yet, have new die if it receives a reference.

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://339243]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-05-08 04:26 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (96 votes). Check out past polls.