Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^4: Private method variations

by adrianh (Chancellor)
on Mar 01, 2004 at 22:11 UTC ( #333071=note: print w/replies, xml ) Need Help??

in reply to Re: Re^2: Private method variations
in thread Private method variations

Perl 6 will have a compromise solution to this. The calling style for a private method is very like, but not quite the same as, the calling style for a public method. All private attributes and method names will start with a distinguishing character, which at the moment I would guess is colon, though at one time it was exclamation mark.

Is this in addition to the submethods mentioned in Apocalypse 6, which seem to solve the same sort of issues?

The self documenting aspect is nice - one of the things I like about Ruby.

(I am so looking forward to A12 :-)

Replies are listed 'Best First'.
Re: Re^4: Private method variations
by TimToady (Parson) on Mar 02, 2004 at 00:57 UTC
    Is this in addition to the submethods mentioned in Apocalypse 6, which seem to solve the same sort of issues?
    Yes. Submethods are for private implementation of a public interface. They're really ordinary public methods that just happen to say "Don't inherit from me, keep looking up the tree for a real method."

    By contrast, a private method cannot be called from outside the class. It is, in fact, just a subroutine that the class declares with a syntax resembling method declaration, and that it can call with a syntax resembling ordinary method call, but there's no dispatcher behind the scenes trying to decide which class to dispatch to. It absolutely calls into the current class. If you use the ordinary dot instead, it absolutely goes to the ordinary dispatcher, which totally ignores all private methods, even in your own class.

      So why would I ever prefer to use a submethod over a private method? This might just be my brain at 1:30am but I can't see a use case that submethod fits better than a private method.

      Any chance of enlightenment or should I just wait for A12/E12 :-)

        Private methods are completely invisible outside of a class, so they cannot participate in the public interface. Submethods can be called from outside a class just like ordinary methods. The only way that submethods are different from methods is that the dispatcher ignores them unless there's an exact match on the class (either because that's the class of the object in question, or because the class was requested as part of, say, a SUPER::.)

        For instance, if you write a constructor such that it can be inherited, then you should declare it a method. But if there are implementation dependencies in your constructor that would make it a bad idea to inherit, then you should declare it a submethod. It's not a private method--you can still call it from outside the class. But any class deriving from your class had better arrange for its own constructor, unless some other class in the tree provides an inheritable constructor.

        In particular, initialization routines that work on your particular set of attributes should submethods. It makes no sense to virtualize such a utility routine, because if you do hierarchical construction and destruction (as Perl 6 will), you'd just end up initializing the same attributes more than once if someone else uses your routine on their class that derives from your class. On the other hand, the routine needs to be callable from outside the class, or your child classes can't walk up the class tree to do hierarchical initialization.

        On the gripping hand, classes with a standardized list of attributes can inherit a standard initialization routine that is metadata driven, and hence class independent. But it's probably slower than if you hard-wired it for your class. Submethods allow both implementation inheritance and implementation hiding under the same public interface.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://333071]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-08-21 09:09 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (319 votes). Check out past polls.