Note: None of this is implemented.

Function references are useful when you want to generalize some piece of code, such that part of its logic is performed by one of several compatible functions selected at at runtime.

Use the ampersand operator to borrow a reference to any existing function:

fn incr(nbr u32) u32:
  nbr + 1

fn doit():
  imm fnref = &incr  // fn ref points to the function "incr"

As with any borrowed reference, this reference may be copied around between variables and functions. It carries no lifetime restrictions, as there is no risk of a global function going away.

The reference may be used at any time to call the function it refers to:

fn doit():
	dostuff(&incr)   // Pass along a reference to the incr function

// Note the parameter needs to declare the reference's function signature
fn dostuff(fnref &fn(u32) u32):
	fnref(4)         // Call the function

The default (and only permissible) permission for a function reference is opaq. This permission does not allow the reference to be dereferenced in any way, thereby prohibiting the ability to read or change the logic of the function it points to.

Anonymous Functions

When the function you want to get a reference to is small, it may be more convenient to specify the logic for the function right where the function reference is to be captured. This is called an anonymous function, as the function has no name:

fn doit():
	dostuff(&fn(nbr u32) u32 {nbr+1})
	dostuff(&fn(nbr u32) u32 {nbr+nbr})