Titan



select

The select is a conditional statement that is used to branch program execution based on the value of an expression. The expression is evaluated and the resulting value is matched against templates specified within the cases. The execution continues with the statement block of the case matching first.

Unlike to the if statement where the way forward is determined with boolean conditions, in the select statement the branching is determined using template matching.

It is important to mention that although the select statement resembles to the alt statement, these differ significantly. The evaluation of the select statement does not involve snapshot handling thus it never blocks execution.

Related keywords:


select ( expression){  cases };

case ( matchings ){  statement_block };

case else {   statement_block };


Examples:


Example 1:

select (v) {
case (0) { log("zero") }
case ( (-infinity..-1) ) { log("negative") }
case ( (1..infinity) ) { log("positive") }
}

The above select statement performs sign check using inline value range templates and logs the result.

Example 2:

select (myMessage) {
case (tr_Message1, tr_Message2, tr_Message3) { log("1st") }
case (tr_MessageA, tr_MessageB) { log("2nd") }
case else { log("none") }
}

When the message stored in myMessage variable matches templates tr_Message1, tr_Message2 or tr_Message3 then the statement block of the first case is executed. If myMessage matches tr_MessageA or tr_MessageB then the second statement block is executed. Otherwise, when none of the enlisted templates match myMessage then the statement block of the case else branch is executed.


BNF definition of select