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.
State Declaration​
A reactor declares a state variable as follows:
The type can be any of the same forms as for a parameter.
The <value>
is an initial value and, like parameter values, can be given as an expression or target language code with delimiters {= ... =}
. The initial value can also be given as a parameter name. The value can be accessed and modified in a target-language-dependent way as illustrated by the following example:
This reactor has an integer state variable named count
, and each time its reaction is invoked, it outputs the value of that state variable and increments it. The reaction is triggered by a timer
, discussed in the next section.
Reset State Variables​
The reset
keyword is not supported in $target-language$ because modal reactors are not supported.
The reset
keyword is not supported in $target-language$ because modal reactors are not supported.
The reset
keyword is not supported in $target-language$ because modal reactors are not supported.
A state variable declaration may be qualified with a reset
keyword as follows:
When this is done, if the state variable or the reactor is within a mode of a modal reactor, then when the mode is entered via a reset transition, the state variable will be reset to its initial value. For details, see the Modal Reactors section.
A state variable declaration may be qualified with a reset
keyword as follows:
When this is done, if the state variable or the reactor is within a mode of a modal reactor, then when the mode is entered via a reset transition, the state variable will be reset to its initial value. For details, see the Modal Reactors section.