Operator parameter modes
A parameter declaration in a composite operator definition must specify an expression mode, and can optionally specify a default value.
composite ThresholdFilter(output Filtered; input Unfiltered) {
param expression<float32> $threshold : 5.0w;
graph stream<float32 x> Filtered = Functor(Unfiltered) {
param filter : x > $threshold;
}
}
The declared type of parameter $threshold
is expression<float32>
.
This type serves as documentation and helps produce better error message.
For example, if the composite operator is used with the wrong type:
stream<float32 x> B = ThresholdFilter(A) { param threshold: x < 3.1w; }
then the SPL compiler gives the error message: Operator parameter 'threshold' expects values of type 'float32', found value of type 'boolean'.
Besides
the expression mode expression<T>
, where T
is
a concrete runtime type, the expression mode for a parameter to a
composite operator can also be just expression
without
a type parameter, indicating that an expression of any type works if
the expansion type-checks. In the previous example, if the declared
type of parameter $threshold
was expression
without <float32>
,
then the error would be detected during operator expansion, and would
refer to internals of the operator:
Type mismatch 'float32 > boolean'.
In
addition to expression
, parameter type declarations
can also use other expression modes:
expressionMode ::= ‘attribute'
| ‘expression' typeArgs?
| ‘function'
| ‘operator'
| ‘type'
typeArgs ::= ‘<' type+, ‘>'
A parameter with expression mode attribute
is
a (simple or qualified) attribute of a tuple type. It is a compiler
error if the parameter gets bound to an attribute of a different tuple
type in the invocation of the composite versus in the body of the composite.
Parameters with expression modes function
, operator
,
and type
refer to (SPL or native) functions, (primitive or composite)
operators, and types.
Still, using the same example, the declared
default value of parameter $threshold
is 5.0. This
default value makes it easier to use the composite operator, because
the user can omit the parameter. Parameters to composite operators
are optional if they have a specified default value, and mandatory
otherwise. For example, the following invocation is short yet complete.
stream<float32 x> B = ThresholdFilter(A) { }
This example is equivalent to providing the default. Besides making operators more reusable, default values also serve as documentation because they give an example for the kind of value that is expected by a parameter.
The syntax for composite operator parameter declarations is:
compositeFormal ::= expressionMode ID ( ‘:' opActual )? ‘;'