A GTK+ one-liner with call chaining
'Tis the dream of each programmer
Before his life is done,
To write three lines of APL
And make the damn thing run.
Call chaining just got better. It used to allow you to do things like:
fw write(“Hey, my name is “). write(name). write(“, what’s yours?”). flush()
But it used to be restricted on a scope-level. Now it’s not anymore, which means you can use and abuse it in expressions, e.g.
setPosition(Vector2 new(). set(20, 50). mul(3))
A few ground rules: the ‘.’ should be thought as an ‘also, do that before.’. So the line above reads as “Pass a new Vector to setPosition. Also, set it to 20, 50 and multiply it by 3 before passing it.”
You can even use it in variable declarations:
win := Window new(). setTitle(“Eclipse”). setBloated(true)
We put a dot after the new() to signal the end of our variable declaration. Every call after will be applied to ‘win’.
Well, it turns out it’s possible to build a whole graphical user interface in one statement with that technique. Read the link. You’ll be surprised.
Chaining may very well be extended to property setting in the future, who knows? Stay tuned.