You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is not a special form but rather a pattern that the switch can implement. By sending a true literal as a condition, the switch will execute the first branch that evaluates to true. Example:
However, switch also allows multiple senders on the same branch. In this case, they are handled like a union. The switch compares the data message with every condition on a branch before moving to the next one. As soon as a match is found, it selects the branch and sends the data message further downstream.
It might not be immediately clear that multiple condition senders are not fan-in, as the syntax might suggest. However, introducing a different syntax for this use-case would be overwhelming, and having fan-in semantics here does not make sense.
Just like with if, all features of switch, including those described below, can be combined in various ways. For example, it is possible to have both multiple condition senders and multiple branch receivers simultaneously.
Similar to if, the switch can have a final receiver connected to its body. Just like if, it receives a message from the data_sender after the selected receiver(s) have received their messages.
In that case, the final receiver will receive a message of type SwitchResult<T, Y>, where data is the message from the data sender and cond is the message from the selected condition branch sender.
{
data T
cond Y
}
Selector
Finally, similar to if and like all routers, the switch has a "selector-mode" where it not only selects a branch to send but also selects a message to send. This is a natural extension of its previous form "with extra inport":
Multiple condition senders and multiple branch receivers are also supported. This means you can have multiple conditions determining which branch to take, and each branch can have multiple receivers.
Notice that multiple data senders are not supported. If you think about it, it will actually make sense. We select a message and direction based on the condition, so we need to select a single message to send.
The more parts a switch has, the more latency it can have. It must wait for the condition sender, all condition and data branch senders, wait for the selected receivers, and finally wait for the final receiver, before receiving the next message.
The text was updated successfully, but these errors were encountered:
Previous part - #802
Switch
Basic
Switch is a more advanced version of
If
that can handle all data types, not just booleans, and can have as many branches as needed.Example
Switch can emulate all possible forms of If. Here is the simplest example:
Switch-True
This is not a special form but rather a pattern that the switch can implement. By sending a
true
literal as a condition, the switch will execute the first branch that evaluates to true. Example:One Branch, Many Senders and Receivers
Just like
if
, theswitch
statement supports multiple receivers on a single branch, functioning as a fan-out.However, switch also allows multiple senders on the same branch. In this case, they are handled like a union. The switch compares the data message with every condition on a branch before moving to the next one. As soon as a match is found, it selects the branch and sends the data message further downstream.
Just like with
if
, all features ofswitch
, including those described below, can be combined in various ways. For example, it is possible to have both multiple condition senders and multiple branch receivers simultaneously.With Final Receiver
Similar to
if
, theswitch
can have a final receiver connected to its body. Just likeif
, it receives a message from thedata_sender
after the selected receiver(s) have received their messages.With Extra Inport
Similarly to
if
, theswitch
can route one message based on another:In that case, the final receiver will receive a message of type
SwitchResult<T, Y>
, wheredata
is the message from the data sender andcond
is the message from the selected condition branch sender.Selector
Finally, similar to
if
and like all routers, the switch has a "selector-mode" where it not only selects a branch to send but also selects a message to send. This is a natural extension of its previous form "with extra inport":Multiple condition senders and multiple branch receivers are also supported. This means you can have multiple conditions determining which branch to take, and each branch can have multiple receivers.
The more parts a switch has, the more latency it can have. It must wait for the condition sender, all condition and data branch senders, wait for the selected receivers, and finally wait for the final receiver, before receiving the next message.
The text was updated successfully, but these errors were encountered: