Syntax and First-Class Functions

For a few years Ruby has been my language of choice for web and CLI projects. Its conceptual and syntactic integrity (every value is an object, every function is a member of a class, every class is a descendant of the Object class) simplifies the work both of thinking through a problem and of expressing your solution. The simplicity and consistency of its design make thinking and writing in The Ruby Way very easy.

But that way is not quite ideal. In particular, the way Ruby handles first-class functions feels relatively clumsy. Here’s a contrived situation in which you might want to use first-class functions:

Ruby requires you to notate the function name that you want to call as a symbol, and then to send that symbol to the object for execution. This adds bad funk to the flow, a small brainfart to the mental model of function-as-variable, by introducing a sort of referent-translation step. You cannot call the variable function directly, as with something like this:

style = informal
Greet.style("neighbor")

Instead, you pass an encoded version of the function’s name to a function that decodes that name and then calls that named function for you.

So on one hand, Ruby’s style is consistent—every method called on an object must be named directly—and its consistency is one of Ruby’s virtues. On the other hand, the process of symbolizing the method name and invoking it with send is less elegant than it could be.

This process of calling a function by a representation of its name is similar to PHP’s call_user_func.

Appending parentheses to a variable name is a fine way of calling a variable function. It looks just like an ordinary function call—there’s no need to call a global function or to prepend a symbolized function name to the parameter list—and it introduces no referent-translation step. And there’s the additional bonus of explicitly indicating an absence of arguments.

If parentheses triggered function calls in Ruby, rather than only designating parameter lists, then its first-class function-handling would be on par with JavaScript’s, which is great. Instead, it’s closer to PHP’s—and not even current PHP, but a deprecated version. It pains to me write that.

But the gold standard of simplicity and consistency is Lisp—any Lisp, but Scheme in particular—where everything is either an atom or a list, and any atom at the start of a list is treated as a function and any others are arguments to that function.