REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Functions: Reflective Properties

Pending Revision

This document was written for R2 and has yet to be revised for R3.

The specification of all functions can be obtained and manipulated during run-time. For example, you can print the specification block for a function with:

probe third :if
[
    "If condition is TRUE, evaluates the block."
    condition
    then-block [block!]
    /else "If not true, evaluate this block"
    else-block [block!]
]

The body code of functions can be obtained with:

probe second :append
[
    head either only [
        insert/only tail series :value
    ][
        insert tail series :value
    ]
]

Functions can be dynamically queried during evaluation. This is how the help and source functions work and how errors messages are formatted.

In addition, this feature is useful for creating your own unique versions of existing functions. For example, a user-defined print function can be created that has exactly the same specification as print, but sends its output to a string rather than the display:

output: make string! 1000

print-str: func third :print [
    repend output [reform :value newline]
]

The name of the argument used for print-str is obtained from the interface specification for print. You can examine that specification with:

probe third :print
[
    "Outputs a value followed by a line break."
    value "The value to print"
]


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin