Documentation for gololang.Decorators

This module defines the set of standard decorators and utilites.

Functions

any()

Void checker: does nothing. Useful when one argument should not be checked.

asChecker(f, m)

Factory function to create a generic checker from a boolean function. Takes a boolean function and an error message.

checkArguments(preTests...)

Checks arguments of the decorated function. Any checker can be used

checkResult(postTest)

Checks result of the decorated function. Any checker can be used

defaultContext()

Returns a void context to be used with the withContext decorator after redefinition of some methods.

A context is an object with 4 defined methods:

In this default version, entry and exit return their parameters unchanged, catcher rethrow the exception and finallizer does nothing.

greaterThan(m)

Factory function to create value checker. Takes a threshold and returns a checker verifying that the given value is greater than the threshold

let isPositive = greaterThan(0)

isNegative()

Value checker function

isNotNull()

Value checker function

isNumber()

Type checker function

isOfType(t)

Factory function to create type checker. Takes a type to compare to and returns a checker function.

let isInteger = isOfType(Integer.class)

isPositive()

Value checker function

isString()

Type checker function

lengthIs(l)

Factory function to create a checker asserting that the length of its argument is exactly a value

@checkResult(lengthIs(2))
function foo |a| -> [a, a]

lessThan(m)

Factory function to create value checker. Takes a threshold and returns a checker verifying that the given value is less than the threshold

let isNegative = lessThan(0)

loggerDecorator(logger)

Factory function returning a decorator that log messages on entry and exit of the function. The factory take the logging function (e.g. println). The returned decorator takes two strings: the message to log before the call, and the message to log after the call. If one of these message is null or empty string, nothing is logged.

let myLogger = loggerDecorator(|msg| {println("# " + msg)})

@myLogger("entering foo", "exiting foo")
function foo = { println("doing foo") }

memoizer()

Factory function for memoization decorator Returns a new memoization decorator. The cache key is the decorated function and its call arguments, thus the decorator can be used for every module functions. It must however be put in a module-level state, since in the current implementation, the decoration is invoked at each call.

let memo = memoizer()

@memo
function foo = |n| -> ...

@memo
function bar = |a,b| -> ...

printLoggerDecorator(prefix, suffix)

A convenient factory to create a loggerDecorator that println with a prefix and a suffix.

@printLoggerDecorator("# ", " #")("in", "out")
function bar = { println("bar") }

withContext(context)

withContext decorator:

@withContext(myContext)
function foo = |a| -> 2*a

This decorator is a very generic one, all the customization occurs in the context object.