Decorators

Introduction

Decorators 1) is a meta-programming feature. Decorator is a function aimed to modify another function or method. Usually decorator function creates another function that wraps call of original decorated function with some additional pre- and post-processing.

In TIScript decorator function is an ordinary function that accepts one or more parameters. First parameter is always reference to the function that is being decorated. Name of decorator function shall start from character '@'.

Here is an example of the @returns decorator. This decorator creates proxy function that does verification of returned value of the original, decorated function:

"Function %s expected to return %s but got %s"

When declared such decorator can be used to ensure that some function returns value of desired type:

 

Syntax of decorator invocation

Core syntax of TIScript was extended to support functions and methods with decorators. Function declaration now looks like:

[<decorator-list>] function <name> ( <arguments > ) { <function body> } -- named function;
[<decorator-list>] : <arguments > : <statement> -- anonymous lambda statement;
[<decorator-list>] : <arguments > { <function body> } -- anonymous lambda function. 

Where the decorator-list is a list of one or many decorator calls. Each decorator call is a name of decorator function (starting from '@') and optional whitespace separated list of expressions - values of parameters of the decorator invocation.

@decoratorname [p1 [p2 [... pN ]]] <function-or-lambda-declaration>

Empty decorator

Sometimes you would need to use empty decorator. Empty decorator ends with ';' :

@decoratorname [p1 [p2 [... pN ]]] ;

The decorator function

Name of decorator function must start with character '@' in order to be able to be used as a decorator.

Decorator function must have at least one parameter, this parameter (first one) is used to pass reference to the function being decorated into the decorator.

this

this environment variable inside the decorator refers to the current namespace/class where the decorator is applied. If decorator is used for global function then this refers to global namespace object.

1) Decorators in TIScript are modeled from decorators in Python programming language
tiscript/decorators.txt · Last modified: 2009/01/11 02:51 by andrew
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0