21: Delegates ∕ Function Pointers - royal-lang/rl GitHub Wiki

Delegates and function pointers are similar in nature, in fact a delegate is a function pointer that has a context pointer. This allows delegates to access local variables whereas a raw function pointer can't.

To get a function pointer from a function simply call the .ptr property on the function.

To get a delegate from a function simply call the .delegate property on the function.

delegate FUNCTION_DECLARATION();
fnptr FUNCTION_DECLARATION();

Example:

delegate int foo();
fnptr int bar();
fn foo() { ... }

fn main()
{
    fn bar() { ... }

    var barDelegate = bar.delegate;
    var fooFunctionPointer = foo.ptr;

    barDelegate(); // Calls bar();
    fooFunctionPointer(); // Calls foo();
}