next up previous
Next: 5 Control Flow Up: CLANGER : an interpreted Previous: 3 Method Invocation

4 Function Definition

One can define functions in CLANGER. A function definition looks like this:

def myFunction[ arg1, arg2, arg3 ]
    returns [ res1, res2, res3 ]
    { ... }
This statement defines a new function with three formal parameters called arg1, arg2 and arg3, and which executes the code inside the curly brackets in an environment almost identical to the caller's except with the actual arguments bound to the names of the formal parameters. The function itself is an Nemesis interface of type ClangerFunction and is inserted into the name space when it is defined. Thus it can be passed around and manipulated in much the same way as anything else.

A function thus defined can be invoked in a similar way to a MIDDL\ method invocation, for example:

[ res1, res2, res3 ] <- myFunction[ arg1, arg2, arg3 ];

The body of the function is just plain CLANGER code. The function must return by using the return statement:

return [ 1, 4, "Hello" ];
The return statement must include the right number of return values. As with normal assignment, the types of values returned will be deduced at run time.

It should be noted that invoking a function call in CLANGER is simply a matter of creating a suitable name space and passing it to the ClangerFunction interface. The name space (in effect the scope for the function) can trivially be constructed by creating a new Context for the variables, and then creating a union of it and the current root. This operation is sufficiently lightweight to allow functions to be used as syntactic sugar for commonly-used invocations.

A possible future extension would allow the user to define complete implementations of MIDDL interfaces, rather than isolated functions not belonging to any interface. While aesthetically appealing, it is unclear how useful this facility would be.



T. Roscoe