Operator logic-state variable naming conventions
Logic-state variables are defined in the body of an operator invocation and hold a state that persists for the lifetime of the operator.
State variable identifiers indicate a property of the data they maintain, and they shadow attribute names. When tuple attributes and state variables have the same names, the identifier (if used unqualified) refers to the state variable.
- Use identifiers in camel case with the first letter written in lowercase (countLHS).
- Write short identifiers with all characters in lowercase.
- Use the fully qualified tuple attribute name in operators with logic-state variables.
This example shows an operator (Counter) that forwards all incoming data, adding to them an attribute with an internal counter value (lines 11-15). In this example, the state variable count shadows the attribute count from the stream Source (lines 11 and 13).
01: composite Main {
02: graph
03: stream<int32 count> SrcA = Beacon() {}
04: stream<int32 count, int32 inCount> CountSrcA = Counter(SrcA) {}
05: }
06:
07: composite Counter(input Source; output Out) {
08: graph
09: stream<int32 count, int32 inCount> Out = Custom(Source) {
10: logic
11: state : mutable int32 count = 0; // shadows variable Source.count
12: onTuple Source: {
13: count++; // refers to the state variable count
14: submit({count = count, inCount = count}, Out);
15: }
16: }
17: }
This next example shows that the tuple attribute is referred to by its fully qualified name, completely avoiding the shadowing (line 15).
01: composite Main {
02: graph
03: stream<int32 count> SrcA = Beacon() {}
04: stream<int32 count, int32 inCount> CountSrcA = Counter(SrcA) {}
05: }
06:
07: composite Counter(input Source; output Out) {
08: graph
09: stream<int32 count, int32 inCount> Out = Custom(Source) {
10: logic
11: state : mutable int32 count = 0;
12: onTuple Source: {
13: count++;
14: // Source.count - unambiguous, fully qualified name
15: submit({count = Source.count, inCount = count}, Out);
16: }
17: }
18: }