F.E.L.T.

Functionally Equivalent Language Translation

RETURN

(return [ expression ] )    ; return with optional expression
(return 42)
(return (foo 3.314))
(return*)                   ; return 'this', for chaining

See also CALL CLASS-CALL/::

This is the class 'return from a function' instruction. It can be used with or without an expression. The expression is evaluated at run-time. For class authors, use return* to ensure the 'this' pointer is returned. This makes it easy to produce methods that can be chained together.


Use in Functions

There isn't much to say about this instruction; used with an optional expression it will cause that expression to be evaluated at run-time and the resulting value will be passed back to the call site.

(return)
(return "Hello World");
(return (file-get-contents filename))

The above simple examples generate the following PHP code;

return;
return "Hello World";
return file_get_contents($filename);

And the JavaScript code:

return;
return "Hello World";
return file_get_contents(filename);

Yup, the same code in this case but remember that unless there is an implementation of file_get_contents at run-time this javascript will fail!

Use in class Methods

If you are writing a class (using CLASS) and you want to produce a set of methods that can be chained then FELT provides a convenience form, return* that causes the 'this' pointer to be returned, whatever that means for the selected back-end target coder!

(class Foo
  (defun say (message)
    (emit message)
    (return*)))

In PHP the following is produced for the above class definition:

class Foo {
  function say($message) {
    echo $message;
    return $this;
  }
}

And for JavaScript:

function Foo () {
  this.say = function(message) {
    console.log(message);
    return this;
  }
}