Squirrel construct used in bullwinkle

I"m looking at the bullwinkle RPC utility to use it instead of our (chaotic) RPC handling mechanism. It’s a nice piece of code but I’d love to understand its intrinsics first. There a particular Squirrel construct I’m not sure how to interprete its behaviour. This resembles some newer C# constructs where you can pipe methods one after the other but not sure it is something similar. Can anyone shed some light ?

bull.send("command") .onreply(function(context) { server.log("Received reply from command '" + context.command + "': " + context.reply); }) .ontimeout(function(context) { server.log("Received reply from command '" + context.command + "' after " + context.latency + "s"); }) .onexception(function(context) { server.log("Received exception from command '" + context.command + ": " + context.exception); })

Bullwinkle.send() creates and returns a Bullwinkle_Session object.

Bullwinkle_Session methods onreply, ontimeout and onexception all return this, so what you have above is shorthand for:

session <- bull.send(...) session.onreply(...) session.ontimeout(...) session.onexception(...)

Thanks. That confirms what I thought… Never realised the ‘this’ keyword can be used in such a way. This opens up nice possibilities to write more beautiful and generic code…

And it is, essentially, identical to the same construct in C# (and in other languages*). It’s usually referred to as a “fluent” interface.

  • It’s not available in Erlang – there’s no ‘this’ to return, and Erlang doesn’t have a pipeline** operator, unfortunately :frowning:

** This is the moral equivalent of returning this in functional, rather than OO, languages. For example, Elixir (look for the ‘|>’ operator) and F#. You can use the pipe operator in PowerShell to create fluent interfaces, too. For example, here’s one from my blog.

I come from a c# background, and I’m a big fan of fluent interfaces - I’ve been experimenting with this construct for a while with Aron and am a big fan!