This page is showing examples in the target language CC++PythonTypeScriptRust. You can change the target language in the left sidebar.
Extending a Base Reactor
The Cpp target does not yet support extending reactors.
Lingua Franca supports defining a reactor class as an extension (or subclass), as in the following example:
reactor A { input a:int; output out:int; reaction(a) -> out } reactor B extends A { input b:int; reaction(a, b) -> out }
WARNING: No source file found: ../code/cpp/src/Extends.lf // the cpp target currently does not support reactor extends
reactor A { input a; output out; reaction(a) -> out } reactor B extends A { input b; reaction(a, b) -> out }
reactor A { input a:number output out:number reaction(a) -> out } reactor B extends A { input b:number reaction(a, b) -> out }
reactor A { input a:u32; output out:u32; reaction(a) -> out } reactor B extends A { input b:u32; reaction(a, b) -> out }
Here, the base class A
has a single output that it writes to in reaction to an input. The subclass inherits the input, the output, and the reaction of A
, and adds its own input b
and reaction. When an input event a
arrives, both reactions will be invoked, but, once again, in a well-defined order. The reactions of the base class are invoked before those of the derived class. So in this case, B
will overwrite the output produced by A
.
One limitation is that a subclass cannot have ports, actions, or state variables with the same names as those in the base class. The names must be unique.
A subclass can extend more than one base class by just providing a comma-separated list of base classes. If reactions in multiple base classes are triggered at the same tag, they will be invoked in the same order that they appear in the comma-separated list.