branch
A decision a component makes inside a local protocol by evaluating guards. It runs the arm whose guard holds.
component Radar;
component CommandPost;
message struct TrackReport {
track_id: uint32;
bearing_deg: float32;
}
message struct Alert {
track_id: uint32;
}
message struct Ack {
track_id: uint32;
}
local protocol Screen in CommandPost {
var alerts: int32 = 0;
recv let report: TrackReport from Radar;
branch
| report.bearing_deg > 180.0 =>
set alerts = alerts + 1;
send any Alert to Radar;
| report.track_id == 0 => send any Ack to Radar;
| else =>
end
}Arms
Each arm is a |, a guard, =>, and the statements to run. The block closes with end, not a brace.
- A guard has type
bit. - Exactly one arm runs.
| else =>is optional and may appear only as the last arm. Without one, a decision where no guard holds has no arm to take. The guards are not retried, so the component is stuck at the block, and so is every component waiting on a message an arm would have sent.- Arms are not tried in order. Where two guards hold, either one may run.
- An arm's statements are the local protocol's own, so an arm may hold a
send, a nestedbranch, abreak, or nothing at all.
Errors
A guard that is not bit
component Radar;
component CommandPost;
message struct TrackReport {
track_id: uint32;
}
message struct Ack {
track_id: uint32;
}
local protocol Screen in CommandPost {
recv let report: TrackReport from Radar;
branch
| report.track_id => send any Ack to Radar; // error: branch-guard-type
| else =>
end
}An else arm that is not last
component Radar;
component CommandPost;
message struct TrackReport {
track_id: uint32;
}
message struct Ack {
track_id: uint32;
}
local protocol Screen in CommandPost {
recv let report: TrackReport from Radar;
branch
| else => // error: branch-else-not-last
| report.track_id > 0 => send any Ack to Radar;
end
}Related
- listen — the same shape, with each arm guarded by an arriving message.
- choice — the same decision in a global protocol, where the other components learn the outcome from the messages they receive.
- if — the same decision between two expressions.
- loop, break — the repetition a branch usually decides to leave.