case instructions

case instructions are used to select and execute one code block among a set of code blocks. Which code block is selected depends on some properties of an expression evaluated at run-time.

case instructions work in a similar manner as pattern matching in other languages.

There are four kinds of case instructions.

Summary:

case type of instruction

The case type of instruction is used to select and execute a block of code depending on an expression’s type. In other words, the instructions to be executed depend on the expression’s type.

Example: Suppose that type product has three sub-types: fruit, vegetable and book. And shopping_list is of type list<product>, and contains any numbers of product objects. Then the following code will count and display how many products of each type are contained in the list:

const shopping_list list<product> = get_shopping_list

variable book_count, fruit_count, vegetable_count = 0

repeat for each product in shopping_list

   case type of product
      when fruit
         fruit_count = fruit_count + 1
      when vegetable
         vegetable_count = vegetable_count + 1
      when book
         book_count = book_count + 1
   .
.

write_line ( """Content of shopping list:
fruit items    : {{fruit_count}}
vegetable items: {{vegetable_count}}
book items     : {{book_count}}""" )
Note
To reduce error-proneness, the compiler checks that each child-type of the expression’s type appears exactly once in the when clauses, unless the last clause is an otherwise clause (see below). Hence, if a child-type is added later, then all case type of instructions must be adapted so that the new type is covered in a when clause.

The case type of instruction is often used with union (sum) types, especially in case of functions that return a result or an error.

Example: Suppose that function get_URL_content returns a string in case of success, and an error object in case of a runtime-error (e.g. network error). Then the result could be checked as follow:

case type of get_URL_content
    when string s
        write_line ( """URL content: {{s}}""" )
    when error e
        write_line ( """Error: {{e.description}}""" )
.

case enum of instruction

The case enum of instruction is used to select and execute a block of code depending on the value of an expression whose type is enumerated. In other words, the instructions to be executed depend on the enumerated value at runtime.

Example: Suppose the following enumerated type exists:

enum type product_size

   small, medium, large, "very large"
.

Then a coefficient used to calculate transportation costs could be computed as follows:

variable coefficient float_64

case enum of product.size

    when small
        coefficient = 0.95
        %wl ( "small product" )

    when medium
        coefficient = 1.0
        %wl ( "medium-size product" )

    when large, "very large"
        coefficient = 1.2
        %wl ( "large or very large product" )
.
Note
To reduce error-proneness, the compiler checks that each enumerated value appears exactly once in the when clauses, unless the last clause is an otherwise clause (see below). Hence, if another enumerated value is added later, then all case enum of instructions must be adapted so that the new enumerated value is covered in a when clause.

case value of instruction

The case value of instruction is used to select and execute a block of code depending on the value of an expression evaluated at runtime.

Example:

case value of get_random_integer
    when 1
        %wl ( "The value is 1" )
    when 2, 3
        %wl ( "The value is 2 or 3" )
    when <= 10
        %wl ( "The value is between 4 and 10" )
    otherwise
        %wl ( "The value is greater than 10" )
.

case reference of instruction

The case reference of instruction is used to select and execute a block of code depending on the object reference an expression points to at runtime.

Example:

const name_1 = "foo"
const name_2 = "bar"
const name_3 = name_2

variable i = 0

case reference of name_3
   when name_1
      i = 1
   when name_2
      i = 2
   otherwise
      i = 3
.

assert i =v 2

The case reference of instruction is often much faster than the case value of instruction, because only pointers (instead of values) are compared. For example, comparing the values of two long strings (i.e. all the characters contained in the strings) can take a lot of time, but just comparing their pointers is much faster.

otherwise clause

Every case instruction can optionally be terminated by an otherwise clause. The otherwise clause (if present) must appear after all the when clauses.

The instructions defined in the otherwise clause are executed if no when clause is satisfied at run-time.

For an example, see the case value of instruction

when null clause

Every case instruction must have a when null clause if the expression evaluated at run-time can result in null.

Example:

case type of get_object_or_null
    when null
        return "Result is null"
    when string
        return "Result is a string"
    when number
        return "Result is a number"
    otherwise
        return "Result is neither a string nor a number nor null"
.

results matching ""

    No results matching ""