Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by merlyn (Sage)
on Feb 27, 2002 at 00:47 UTC ( #147784=note: print w/ replies, xml ) Need Help??


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

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


Comment on •Re: Re: Mmmm ... cargo cult progamming is nummy!
Download Code
Re: Re: Re: Mmmm ... cargo cult progamming is nummy!
by dws (Chancellor) on Feb 27, 2002 at 04:23 UTC
    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.

        So, to test your theory, I took the latest Squeak Smalltalk image (arguably derived directly from the original Xerox Park Smalltalk80 Image)

        merlyn and I have been doing some digging, which I'll record here in case this discussion resurfaces in the future.

        I overgeneralized when claiming prior art in Smalltalk, and should have claimed prior art in VisualWorks, which was ParcPlace's "fork" of st80 (ParcPlace split off from Xerox Parc to commercialize Smalltalk). I have an image from the VisualWorks 2.5 era which contains an instance-side new, albeit in a dubious chunk of code that was later excised from the image. merlyn has verified that there are no instance-side new methods on VisualWorks 7.

        So, the bottom line is that isn't good, solid prior art in Smalltalk for an instance new method. At best there was a failed experiment.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-17 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (40 votes), past polls