Code is data and data is code
Now we come back to the powerful introspective and metaprogramming features of Red—namely, how Red can work with code in the same way as data.
Using reflection on functions
Functions are first-class objects in Red. You can give your own functions, or even built-in ones, another name, as shown here:
;-- see Chapter06/reflection-functions.red:
pr: :print ;== make native! [[
pr ["Hello""Red"] ;== Hello Red
The :
prevents the function from executing; pr: get
'print
does the same thing.
Note
This shows Red's flexibility and power; not only changing the name, but even changing the meaning of built-in words is possible. As always, with great power comes great responsibility. Use these and the other metaprogramming features to come sparely, and above all, wisely!
Functions can contain other functions that are local to that function and that can access all its variables.
A function can also return a function as a result, as shown in the following code example:
make...