Method Chaining

I was quite surprised that my search on this site didn't turn up any results.

The pattern seems to be ubiquitous, I've seen it in jQuery and Magento, to name but two frameworks that come to mind. I'm sure it's in RubyOnRails, too.

Related to FluentInterface?, it makes for concise code at the call site.

On the other hand, it seems to me that its realization in the most common languages is slightly unelegant - manually replacing any void return type with the type of "this" seems clumsy and error-prone (i.e., easy to forget).

It has long seemed to me that this should be a language feature. Good ol' Pascal had the "with" keyword. Now I read that SmalltalkLanguage has ";" for sending multiple messages to the same object. A language might even choose to replace the "void" return type with an implicit "this" return value to support it.

--ChristianFriedl


EditText of this page (last edited September 18, 2013) or FindPage with title or text search