-
Book Overview & Buying
-
Table Of Contents
Learn Bosque Programming
By :
Another way of constructing conditionals is through the switch statement, which simplifies writing code in scenarios where several evaluations must be carried out on the same variable.
But additionally, Bosque has implemented a series of extra functions to enhance this statement using pattern matching, constraints, and type-based dispatch.
First, let's look at the most basic example:
switch(x) {
case 1 => { return "option one"; }
case 2 => { return "option two"; }
case _ => { return "any other option"; }
}
As we see in the code, the switch block allows us to execute different instructions according to the value of the variable x. The wildcard _ will enable us to establish a flow for the values that do not correspond to any defined cases.
As we mentioned earlier, Bosque also allows us to use switch blocks to evaluate types, as shown...