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

Re: Mmmm ... cargo cult progamming is nummy!

by dws (Chancellor)
on Feb 27, 2002 at 00:25 UTC ( [id://147776]=note: print w/replies, xml ) Need Help??


in reply to Mmmm ... cargo cult progamming is nummy!
in thread Adding autoloaded methods to symbol table with using strict refs

What that's really doing is co-opting your constructor (typically called new()) and using it as a copy function, (typically called copy() or clone()).

I have rarely seen the equivalent of a copy constructor in someone's new method. Rather, I've seen the   my $class = ref $proto || $proto; trick used to add "make me a new one of these" semantics to the standard "make me a new instance of this package" semantics.

If you're restricted to created instances of a named class, you have extra hoops to jump through to extend your software to accomodate new subclasses. By adding a "make me new one of these" capability, you defer past compile-time the decision of what package to create a new copy of.

Replies are listed 'Best First'.
•Re: Re: Mmmm ... cargo cult progamming is nummy!
by merlyn (Sage) on Feb 27, 2002 at 00:47 UTC
    But that can be done by the caller, in a clearer fashion:
    my $new_thing = (ref $this_thing)->new(@params);
    This clearly says to me "for the class of $this_thing, call new". Very clear.

    You don't need to put (cargo-cult fashion) the "ref $proto || $proto" device in every single constructor. The caller can control this.

    If you want to provide a clone method in your class, do so. But don't try to make "new" do double duty. It confuses those of us with some common sense.

    -- Randal L. Schwartz, Perl hacker

      If you want to provide a clone method in your class, do so. But don't try to make "new" do double duty. It confuses those of us with some common sense.

      Again, it's not a clone method, and there is prior art on this from Smalltalk. I'd be surprised if you haven't run across it. A double-duty new is basically simulating what Perl cannot do directly, which is to separate instance-side from class-side methods.

      I'll let the bit about confusing you with some common sense slide for now.

        {...} there is prior art on this from Smalltalk. I'd be surprised if you haven't run across it. {...}
        So, to test your theory, I took the latest Squeak Smalltalk image (arguably derived directly from the original Xerox Park Smalltalk80 Image), and ran a few tests:
        Metaclass allInstances size -> 1280
        So there are 1280 classes in the current image. Now how many of them understand "new" as an instance method:
        Metaclass allInstances select: [:m | m soleInstance includesSelector: +#new] -> #(Behavior class Metaclass class)
        There. The only two classes that have instances that understand #new are the things that act as factories: Behavior and Metaclass. And the code for Metaclass instances basically just says "don't do that" and throws an error. {grin} And the code is in Behavior so that all object classes also know how to #new themselves.

        So I go by my original hypothesis. In classic OO programming, best illustrated by Smalltalk80, the word "new" contains the strict connotation of "make a new instance of", and is applied only to class objects, not to instance objects.

        By contrast, the Object implements both #clone (as a primitive) and #copy (to allow overriding for shallow copy vs deep copy), so every object knows how to clone itself.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-19 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found