First-Class Functions in an Imperative World
Roberto Ierusalimschy (PUC-Rio, Brazil)
Abstract: First-class functions are a hallmark of functional languages, but they are a useful concept in imperative languages, too. Even ANSI C offers a restricted form of first-class functions (in the form of pointers to functions), and several more recent imperative languages, such as Python 3, Go, Lua, and Swift, offer first-class, anonymous functions with lexical scoping.
In most imperative languages, however, first-class functions are an advanced feature used by seasoned programmers. Lua, by contrast, uses first-class functions as a building block of the language. Lua programmers regularly benefit from diverse properties of its functions for routine constructions such as exception handling, module definitions, object-oriented programming, and iterators. Moreover, first-class functions play a central role in the API between Lua and C.
In this paper, we present some aspects of Lua that were designed with first-class functions in mind, in particular its module system, exception handling, iterators, facilities for object-oriented programming, and the API between C and Lua. We also discuss how those different aspects of Lua use different properties of first-class functions to achieve two important goals in the design of Lua, namely small size and embeddability (its easiness of interfacing with other languages).
Keywords: Lua, functional languages, scripting languages
Categories: D.1.1, D.3.3