2.11. Expressions

An expression can be any of the following (note that expressions are also recursively defined):

Table 2.9. Expressions

Type

Description

Examples

 

An immediate value

Qore values that can be expressed directly (see Basic Data Types for more information)

True
152
1.2
"a string"
2005-10-27
NULL
NOTHING
("key" : $val)
1, 2.0, "three"
 

A variable reference

Qore variables (see Variables for more information)

$var
 

An in-class object member reference

References to members of an object from within the class (see Object Members for more information)

$.member
 

An lvalue assignment

Assigns a value to a lvalue (see Assignment Operator for more information)

$var = 1
($a, $b, $c, $date) = (1, "two", 3.3, 2005-10-28)
 

A subroutine/function call

Qore subroutine calls (see Subroutines for more information)

calculate($var1, $var2, "string", 4)
 

A method call

Qore object method calls (see Object_Method_Calls for more information)

$object.method("argument")
 

An in-class method call

Qore in-class object method calls (see Object_Method_Calls for more information)

$.method("argument")
 

A static method call

Qore static method calls (see Static Methods for more information)

ClassName::static_method("argument")
 

Expressions with operators

Use of Qore operators (see Operators for more information)

1 + 2
$a || $b
background my_function()
 

An expression in parentheses

Use of parentheses for clarity or to specify evaluation precedence.

(1 + 2)
(2 * (3 + 1))
 

A list

a list of values (see Lists for more information)

1, 2, 3, "four", 5.0
 

A hash

a hash (associative/keyed array) value container (see Hashes for more information)

( "key1" : 1, "key2" : "two" )
 

A find expression

Finds a value or values in a hash of lists, such as returned by the Datasource::select() method (see find expressions for more information)

find %name, %id in $data where (%name =~ /Smith/);
 

A context identifier (%column)

A contextual reference to the value of a key of a complex data structure within the current row being iterated by a context, summarize, subcontext statement, or a find expression.

%name
 

A context row identifier (%%)

A contextual reference to the current row of complex data structure being iterated by a context, summarize, subcontext statement, or a find expression. This expression will return a hash of the current row.

%%
 

A call reference

A reference to a function or object method call (similar to a function pointer in C or C++). Function references are resolved in the second phase of parsing (commit phase), and object method references are resolved at run-time.

\function_call()
\$object_expression.method_name()
 

A closure

An anonymous function used a value; technically in computer science a closure must have at least one bound variable, but in qore a closure is any function used as a value, whether or not it encloses local variables from the scope in which it was created or not.

sub ($a) { return $a + $b; }
 

A call reference call

Calls the code referenced by the call reference expression or closure using any arguments supplied and returns the result.

$result = $call_reference($arg1, $arg2)
 

An implicit argument reference

References an implicit argument.

$1, $2
$$
 

2.11.1. Static Method Calls

Synopsis

Calls to static class methods are made by giving the class name followed by two colons and then the method name. The method name must be implemented and accessible (i.e. not private and accessed outside the class) somewhere within the class hierarchy and must be static or a parse exception will occur.

Syntax

class_name::method_name( [argument_expressions...] )

Description

class_name

The name of the class implementing the static method.

method_name

The name of the static method to call.

[argument_expressions...]

Expressions passing arguments to the static method.

Example
QDir::setCurrent("/tmp");

2.11.2. Find Expressions

Synopsis

The find expression can be used to quickly find data in a hash of lists (such as a query result set returned by the Datasource::select() method). The find expression will loop through a data structure, and for each element in the structure where the where expression is True, it will evaluate and return a result expression. If the where expression only is true for one element in the list, it will return the result of evaluating the result expression directly, otherwise if the where expression is true more than once, then a list of the results of evaluting the result expression for each element is returned. In each expression in the find expression, column names can be referred to by preceding the name with a '%" character (as with context statements).

Syntax

find result_expression in data_expression where ( where_expression )

Description

result_expression

This expression will be evaluated and returned when the where_expression evaluates to True.

data_expression

This expression must evaluate to a hash of lists, so that the internal context can be set up for the find loop.

where_expression

This expression will be evaluated for each row in the data_expression. Each time it evaluates to True, the result_expression will be evaulated and used for the return value for the find expression.

Example
$rlist = find %name, %id in $data where (%name =~ /Smith/);

2.11.3. Call References

Synopsis

References to functions or object methods are called call references. A call reference can be used like a function pointer; a call reference is a Qore data type that can be returned by functions or methods or assigned to variables.

Note that it is currently not legal to assign a call reference to a constant. This restriction may be lifted in a future version of Qore.

Function Call References

Call references to functions are resolved at parse time; if the function does not exist a parse exception will be thrown.

Object Method Call References

Call references to object methods are executed and resolved at run time; if the object expression does not evaluate to an object at run-time, an OBJECT-METHOD-REFERENCE-ERROR exception will be thrown. If the method does not exist, a METHOD-DOES-NOT-EXIST run-time exception will be thrown.

When called, a call reference to an object method will be executed in the context of the object originally referenced.

Object method call references do not prolong the lifetime of an object; if the object is deleted (for example, by going out of scope), then if called the call reference will cause a OBJECT-ALREADY-DELETED exception to be thrown.

Syntax

\function_name();
\object_expression.method_name();

Description

\function_name()

This form gives a function call references. The function name can be any valid user or builtin function name. Note the backslash at the beginning and the empty pair of parentheses at the end; these are required when specifying a call reference.

\object_expression.method_name()

This form gives an object method call reference. The object expression can be any valid Qore expression that evaluates to an object. The method_name must be an unquoted string (see examples below) and must represent a valid method name of the object's class.

Example
$call_ref = \func_name();
$call_ref = \$object.method_name();

2.11.4. Closures

Synopsis

A closure is an anonymous function used as a value. Closures can be returned from functions or methods, assigned to variables, or passed as arguments to other functions.

Note that it is not legal to assign a closure to a constant.

Syntax

sub () { code... }

Description

Closures encapsulate the state and value of local variables of the outer code block referenced from within the closure when the closure is created. Whenever local variables are bound within a closure, these variables are subject to concurrent thread access protection (locking) just as with global variables, in order to allow closures to be used in any context without restriction and to preseve thread-safety regarding bound local variables.

Note that returning a closure from within an object method encapsulates the state of the object as well (it's legal to refer to $self and $.<variable> from within closures created from objects) and additionally prolongs the scope of the object for the lifetime of the closure.

Example
# if $b is a local variable in the function where the closure is created
# then $b will be bound to the closure when the closure is created
my $closure = sub ($a) { return $a + $b; };

2.11.5. Implicit Argument References

Synopsis

Implicit arguments are arguments not captured by parameter variables as well as automatic arguments in list-processing operator expressions. A special syntax to reference these arguments is documented here.

Syntax

$<integer> # for a single implicit argument
$$         # for the entire implicit argument list

Description

Implicit arguments can be directly referenced using the dollar sign ($) and either a number from 1 onwards (giving the position in the argument list, where 1 is the first element) or a double dollar sign ($$) giving the entire implicit argument list.

For unassigned arguments to functions or methods, this syntax supplements the automatic $argv variable holding all function arguments not assigned to parameter variables.

This syntax is particularly useful when writing expressions for the map, map, foldr, and select operators, where implicit argument references are the only way the operator expressions can reference the current list values that are populated as implicit arguments as the operators traverse the list.

Example
# extract a list of even numbers from a list
my $l = select $list, !($1 % 2);