Parameters and State Variables
This article has examples in the following target languages:
- C
- C++
- Python
- Rust
- TypeScript
Parameter Declaration​
A reactor class definition can parameterized as follows:
Each parameter has a type annotation, written :<type>
, where <type>
has one of the following forms:
- An identifier, such as
int
, possibly followed by a type argument, e.g.vector<int>
. - An array type
type[]
andtype[integer]
. - The keyword
time
, which designates a time value. - A code block delimited by
{= ... =}
, where the contents is any valid type in the target language.
- A pointer type, such as
int*
.
Types ending with a *
are treated specially by the C target. See the Target Language Details.
To use strings conveniently in the C target, the "type" string
is an alias for {=const char*=}
.
For example, {= int | null =}
defines nullable integer type in TypeScript.
Each parameter has a type annotation, written :<type>
, where <type>
has one of the following forms:
- An identifier, such as
int
, possibly followed by a type argument, e.g.vector<int>
. - An array type
type[]
andtype[integer]
. - The keyword
time
, which designates a time value. - A code block delimited by
{= ... =}
, where the contents is any valid type in the target language.
- A pointer type, such as
int*
.
Types ending with a *
are treated specially by the C target. See the Target Language Details.
To use strings conveniently in the C target, the "type" string
is an alias for {=const char*=}
.
For example, {= int | null =}
defines nullable integer type in TypeScript.
Each parameter has a type annotation, written :<type>
, where <type>
has one of the following forms:
- An identifier, such as
int
, possibly followed by a type argument, e.g.vector<int>
. - An array type
type[]
andtype[integer]
. - The keyword
time
, which designates a time value. - A code block delimited by
{= ... =}
, where the contents is any valid type in the target language.
- A pointer type, such as
int*
.
Types ending with a *
are treated specially by the C target. See the Target Language Details.
To use strings conveniently in the C target, the "type" string
is an alias for {=const char*=}
.
For example, {= int | null =}
defines nullable integer type in TypeScript.
Each parameter has a type annotation, written :<type>
, where <type>
has one of the following forms:
- An identifier, such as
int
, possibly followed by a type argument, e.g.vector<int>
. - An array type
type[]
andtype[integer]
. - The keyword
time
, which designates a time value. - A code block delimited by
{= ... =}
, where the contents is any valid type in the target language.
- A pointer type, such as
int*
.
Types ending with a *
are treated specially by the C target. See the Target Language Details.
To use strings conveniently in the C target, the "type" string
is an alias for {=const char*=}
.
For example, {= int | null =}
defines nullable integer type in TypeScript.
Depending on the target, the type may be a generic type, which means that the type is parameter determined at the time the reactor class is instantiated.
Each parameter must have a default value, written <param-name> = <expr>
. An expression may be a numeric constant, a string enclosed in quotation marks, a time value such as 10 msec
, a list of values, or target-language code enclosed in {= ... =}
, for example. See Expressions for full details on what expressions are valid.
For example, the Double
reactor on the previous page can be replaced with a more general parameterized reactor Scale
as follows:
This reactor, given any input event x
will produce an output y
with value equal to the input scaled by the factor
parameter. The default value of the factor
parameter is 2, but this can be changed when the Scale
reactor is instantiated.
Notice how, within the body of a reaction, the code accesses the parameter value. This is different for each target language. In the C target, a self
struct is provided that contains the parameter values.