FunctionChain#

A function chain implements a compile-time observer pattern that allows a module to call back into dependent modules in a decoupled way.

See following example:

using import FunctionChain

# declare new function chain
fnchain activate

fn handler (x)
    print "handler activated with argument" x

'append activate handler

'append activate
    fn (x)
        print "last handler activated with argument" x

'prepend activate
    fn (x)
        print "first handler activated with argument" x

activate 1
activate 2
'clear activate
'append activate handler
activate 3

Running this program will output:

first handler activated with argument 1
handler activated with argument 1
last handler activated with argument 1
first handler activated with argument 2
handler activated with argument 2
last handler activated with argument 2
handler activated with argument 3
type FunctionChain

A plain type of storage type (opaque@ _type).

spice __call ( ... )
inline __repr ( self )
spice __typecall ( ... )
inline append ( self f )

Append function f to function chain. When the function chain is called, f will be called last. The return value of f will be ignored.

inline clear ( self )

Clear the function chain. When the function chain is applied next, no functions will be called.

inline on ( self )

Returns a decorator that appends the provided function to the function chain.

inline prepend ( self f )

Prepend function f to function chain. When the function chain is called, f will be called first. The return value of f will be ignored.

sugar (decorate-fnchain  ... )
sugar (fnchain  name )

Binds a new unique and empty function chain to identifier name. The function chain's typename is going to incorporate the name of the module in which it was declared.