Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I like it. It feels very Smalltalky. You're sending a bunch of messages to the object, so why repeat the invocant? Expressions have results, so why not use them?

In Smalltalk this is called a "cascade", and there's special syntax to support it. In Smalltalk, if you send two messages to the object anObject (i.e., if you invoke two of anObjects's methods), you can use cascading to rewrite

anObject foo. anObject bar.
as
anObject foo; bar.
Both messages get sent to anObject, regardless of what the first method returns. That is, the return value from foo is ignored. The value of this expression is whatever is returned from bar.

Perl has no notion of method call cascading, so to simulate cascading, all member functions in a simulated cascade chain (except that final one) need to return self. This works great if you're making a chain of calls for side-effects, and less great if you're composing an expression from a set of cascaded calls, and are interested in the final value, because of the requirement that all cascadable methods return self.

My opinion is that cascading is an idiom that doesn't translate well into Perl. Without a special syntax to designate a cascade, a simulated cascade is indistinguishable from a gross violation of the Law of Demeter.


In reply to Re: Re: chaining method calls by dws
in thread chaining method calls by perrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-16 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found