REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 12-Aug-2010 Edit History  

REBOL 3 Functions: switch

switch  value  cases  /default  case  /all

Selects a choice and evaluates the block that follows it.


value - Target value

cases [block!] - Block of cases to check



case - Default case if no others found

/all - Evaluate all matches (not just first one)

See also:

case   select   find  


The switch function selects the block for a given choice and evaluates it.

For example:

switch 22 [
    11 [print "here"]
    22 [print "there"]

This function is equivalent to writing a select like this:

do select [
    11 [print "here"]
    22 [print "there"]
] 22

Variety of Datatypes

The selection choices can be of any datatype. Here are some examples:

file: %user.r
switch file [
    %user.r [print "here"]
    %rebol.r [print "everywhere"]
    %file.r [print "there"]
switch url [ [print "here"] [print "there"] [print "everywhere"]
tag: <title>
print switch html [
    <pre>   ["preformatted text"]
    <title> ["page title"]
    <li>    ["bulleted list item"]
page title

Cases Not Evaluated

It's very important to note that the choices are not evaluated (think of them as constants.) This allows the choices to be words, as shown below. If you need evaluation of the case values, use the case function instead.

person: 'mom
switch person [
    dad [print "here"]
    mom [print "there"]
    kid [print "everywhere"]

This most often becomes important when you want to switch based on a datatype value. You must be sure to use type? with a /word refinement:

val: 123
switch type?/word [
    integer! [print "it's integer"]
    decimal! [print "it's decimal"]
    date! [print "it's a date"]
it's integer

Here the type? function returns the word (name) of the datatype!, not the datatype's type value.

Another possible approach is to evaluate the block of cases. For the example above:

switch type? reduce [
    integer! [print "it's integer"]
    decimal! [print "it's decimal"]
    date! [print "it's a date"]
it's integer

This works because words like integer! are set to their actual datatype values.

Default Case

You can use the /default refinement to specify a default case.

time: 14:00
switch/default time [
     8:00 [send wendy@domain.dom "Hey, get up"]
    12:30 [send cindy@dom.dom "Join me for lunch."]
    16:00 [send group@every.dom "Dinner anyone?"]
    print ["Nothing done at" time]
Nothing done at 14:00

Return Value

The switch function returns the value of the case block that it evaluated, or none otherwise.

car: pick [Ford Chevy Dodge] random 3
print switch car [
    Ford [351 * 1.4]
    Chevy [454 * 5.3]
    Dodge [154 * 3.5]

Common Problems

The most common problem is to assume that switch evaluates your case values. It does not. This kind of code does not work:

item1: 100
item2: 200
n: 100
switch n [
    item1 [...]
    item2 [...]

However, you can reduce the case block to its actual values:

switch n reduce [
    item1 [...]
    item2 [...]

  TOC < Back Next > - WIP Wiki Feedback Admin