Search

What the Quote?

"When driving in Jersey, you need to drive confident, so everyone else drives scared."

Steven Rodgers

"You can't polish a turd."

Michael Nurre

"...at least the children we don't have are wearing pants."

Tim Tripcony

« Changing read-only database settings with DXL | Main| New toy: Belkin Wi-Fi Skype phone »

Chaining methods in LotusScript

Category show-n-tell thursday
One characteristic now common to several popular Javascript libraries (such as Mootools, jQuery, and the newly updated Prototype) is the ability to chain methods: that is, to do several things to or with an object in a single line of code. For example:

Clown.makeBalloonAnimal().rideUnicycle().frightenSmallChildren();

This appeals to many of us because, well, we're lazy. That's not a bad thing - the point of code is to make some task or process easier for users; so why not structure code in a way that makes it easier to reference that code later on?

Anyhow, what makes this possible in Javascript is that all methods are functions. Even what LotusScripters would consider a Sub. Which means that all methods have a return value, even if none is explicitly specified. A chainable method, then, is simply a function that returns an object (typically the instance containing the method).

In LotusScript, this is easily accomplished by making each Sub a Function instead and using the keyword Me:

Public Class Clown Public Function makeBalloonAnimal() As Clown 'Method statements Set makeBalloonAnimal = Me End Function Public Function rideUnicycle() As Clown 'Method statements Set rideUnicycle = Me End Function Public Function frightenSmallChildren() As Clown 'Method statements Set frightenSmallChildren = Me End Function End Class

All methods of the class are now chainable:
Sub Initialize Dim Bozo As Clown Set Bozo = New Clown() Call Bozo.makeBalloonAnimal().rideUnicycle().frightenSmallChildren() End Sub

Comments

Gravatar Image1 - Concise and thoughtful - Amazing!
Chapeau bas! Tim

Gravatar Image2 - That is a VERY interesting technique. Thanks for the tip!