This module defines the set of standard decorators and utilites.
Void checker: does nothing. Useful when one argument should not be checked.
Factory function to create a generic checker from a boolean function. Takes a boolean function and an error message.
Checks arguments of the decorated function. Any checker can be used
Checks result of the decorated function. Any checker can be used
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:
entry
: takes and returns the function arguments. Can be used to check
arguments or apply transformation to themexit
: takes and returns the result of the function. Can be used to check
conditions or transform the resultcatcher
: deals with exceptions that occurs during function execution.
Takes the exception as parameterfinallizer
: is called in a finally
clause after function execution.
No parameterIn this default version, entry
and exit
return their parameters unchanged,
catcher
rethrow the exception and finallizer
does nothing.
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)
Value checker function
Value checker function
Type checker function
Factory function to create type checker. Takes a type to compare to and returns a checker function.
let isInteger = isOfType(Integer.class)
Value checker function
Type checker function
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]
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)
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") }
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| -> ...
A convenient factory to create a loggerDecorator
that println
with a prefix and a suffix.
@printLoggerDecorator("# ", " #")("in", "out")
function bar = { println("bar") }
withContext
decorator:
@withContext(myContext)
function foo = |a| -> 2*a
This decorator is a very generic one, all the customization occurs in the context object.