

Reinventing the wheel: the fluent interface design pattern - nimbix
http://lexandera.com/2009/04/reinventing-the-wheel-the-fluent-interface-design-pattern/

======
gruseom
That's totally wrong. The point of this style of programming is allow a series
of function calls to be written as a linear chain of expressions. It comes
from Smalltalk via Eric Evans, who popularized it in the Java world
(<http://www.martinfowler.com/bliki/FluentInterface.html>). This style is
favored by people who agree that side effects should be avoided but who don't
like to build functional expressions with many levels of nesting (à la Lisp).
Evans compared these composite linear expressions to noun phrases in English
and argued that they can be a more natural way to express domain concepts in
code.

The "with" control structure only resembles this in the degenerate case where
every expression in the chain evaluates to _the exact same object_. Not only
that, the "with" keyword expects a series of statements (as opposed to
expressions), and that is the exact opposite of what the chaining style is
about. This is easily seen in the author's own example: each member of the
sequence has to end in a semicolon (i.e. is a statement rather than an
expression), so you don't have a single composite expression at all, only a
slightly more compact procedural script.

~~~
pdubroy
You're right, it definitely comes from Smalltalk. For those who aren't familar
with Smalltalk, you can use the ';' operator to send a sequence of messages to
the same object. The example in the article would look something like this:

    
    
      mainFrame := JFrame new: 'Hello World';  
          add: label withAlignment: BorderLayout.CENTER;  
          addWindowListener: MainFrameListener new;  
          setDefaultCloseOperation: JFrame.DO_NOTHING_ON_CLOSE;  
          pack;  
          setLocationRelativeTo: nil;  
          setVisible: True;
          yourself.
    

(The result of a chained method expression is the result of the last method
call, thus the 'yourself' message at the end so that the object itself is
stored into mainFrame.)

Sigh...I miss Smalltalk.

------
mahmud
"Yes, you’re right! It looks remarkably like the “with” control structure
available in Delphi/Pascal and VisualBasic!"

Those who do not understand Lisp are doomed to reinvent it, poorly. :-)

Fairly good intro article though.

~~~
gruseom
As one dedicated Lisper to another, I think you're missing the point here.
S-expressions are inherently nested, and the chaining style is about avoiding
that (while still being side-effect free). I wrote about this a long time ago
on HN... let me see if I can dig up the link.

Edit: here's one, though I remember writing something else with examples. Oh
well. <http://news.ycombinator.com/item?id=304871>

~~~
mahmud
Good point. One way to do it might be to reify the object instance pointer to
a first-class argument (usually the first.) Another way is with a dynamic
variable _self_ (might be preferable, since you don't have to break function
signatures.)

Yet another option might be with a custom method combination, which is
overkill :-P

Any other ideas? or am I too off the ball park?

~~~
gruseom
Well, the conclusion I came to was that trying to make Lisp less nested would
be like trying to make the North Pole less arctic. If you don't like
programming that way, you probably aren't using Lisp in the first place. There
are a few exceptions, of course (I like loop quite a bit).

