This is a fairly common method in other languages. Almost every high-level function of a Schema is defined in terms of lambda that are invoked immediately.
In JavaScript, this is the core of a “module template,” for example
var myModule = (function() { // declare variables and functions (which will be "private") return { // populate this object literal with "public" functions }; })();
Thus, an anonymous function is declared and immediately called, so any internal data is hidden, and only the external value is displayed outside.
The only drawback is that with random reading of the code, the return seem to be coming back from an external function (during the Yambda wars, Java had intense debate). But this is just what you need to get used to when your language has lambdas.
There are many language functions in an imperative language, such as C ++, that can benefit from being able to return a value (rather than as a void function). For example, if has an alternative, the tertiary operator expr ? a : b expr ? a : b .
In Ruby, almost all operators can be evaluated, so there is no need for a separate syntax where you can specify the return value. If C ++ worked this way, it would mean things like:
auto result = try { getIntegerSomehow(); } catch (const SomeException &) { 0; }
Daniel Earwicker
source share