Function Definition Expressions
As introduced previously, OQL supports functions. There are two types of
functions definition syntax:
function definition expression and function definition statements.
The first ones, exposed in this section, are more restrictive than the
second ones, as their definition can contain only one expression.
The second ones contain an unbounded sequence of statements.
The function definition expressions are ODMG compatible and are called
Named Query Definition in the ODMG standard.
To define such a function, one must use the operator define/as.
The general syntax for a definition function expression is:
define identifier [(arglist)] as expr.
For instance:
define div2(x) as x/2;
div2(10); // returns 5
define pimul(x) as x * 3.1415926535;
pimul(23); // returns 72.256631
define getOnePerson(name) as first(select Person.name = name);
getOnePerson("john"); // returns an oid or nil
As the last operand of the define/as operator is any OQL
expression, it can be a sequence of expressions by using the
comma sequencing operator. Therefore, the following construct is valid:
define getit(age) as ::getit_call++,
list_of_persons := select Person.age >= age,
(count(list_of_persons) > 0 ? list_of_persons[0] : nil);
getit(10); // returns the first Person whose age is greater or equal to
10, or nil
getit_call; // equals 1
list_of_persons; // raises an error: uninitialized identifier 'list_of_persons
getit(20);
getit_call; // equals 2
Several comments about this code:
- ::getit_call denotes a global variable, while
list_of_persons denotes a variable local to the function:
the variable scoping has previously been introduced in the identifier
expressions, and will be explained again in the function definition statements
Section.
- as there are no iteration expression (for instance a for or
while expression) and as a function definition expression can
contain only one expression, one cannot use a function definition expression
with iteration statements.
To use an iteration statement, one needs to use a function definition
statement.
- when one needs to define a function with a sequence
of expressions, it may be easier and more readable to use a
function definition statement instead of a function definition expression.
The statement version of the previous function is:
function getit(age) {
::getit_call++;
list_of_persons := select Person.age >= age;
if (count(list_of_persons))
return list_of_persons[0];
}
which is - for C, C++ or Java programmers -
a more natural construct.
Finally, the function definition allows us for recursive definitions,
for instance:
define fib(n) as (n < 2 ? n : fib(n-2) + fib(n-1));
fib(10); // returns 55
General Information |
Operator |
define/as |
Syntax |
define identifier [arglist] as expr |
Type |
n-ary |
Operand Type |
first operand: identifier,
last operand: any type, other operands: identifier |
Result Type |
identifier |
Functions |
defines a function |
Expression Examples |
define Persons as select Person |
Persons |
define fact(n) as (n < 2 ? n : n * fact(n-1)) |
fact |
EyeDB manual