The case type of instruction is used to select a set of instructions to execute, depending on a target object's type at runtime.
![]() | Note |
|---|---|
Although polymorphism is certainly the preferred OO way to define different behavior for different types of objects, there are also cases where the type of an object must programmatically be analyzed at runtime. An example would be the need for a specific treatment of objects in a list of objects of type any_type. |
The case type of instruction has the same effect than an if then elseif instruction that would test the type of an object and then execute different instructions, depending on the type of the object. However, the case type of instruction has a number of benefits because the compiler makes a number of useful checks to detect some potential coding errors. Suppose that fruit, vegetable and book are child types of product, and that the target object in the case type of instruction is of type product. In that case the compiler checks that each subtype of product (i.e. fruit, vegetable and book) appears exactly once in the case type of instruction, and it refuses any type in the case type of instruction that is not a child type of product. Hence, if the programmer forgets a child type, or uses the same child type multiple times, or uses a type which is not a child type, the compiler generates an error. Even more importantly, whenever another subtype of product (i.e. game) is added later, the compiler detects any case type of instruction that has not been adapted to cover the case of a product object being a game.
![]() | Note |
|---|---|
The compiler only ensures that each childtype appears exactly once if the target object is not of type any_type, and if the otherwise clause is not used in the case type of instruction. |
Table 8.10. case type of syntax
| Production | Syntax | Links |
|---|---|---|
case_type_of_instruction |
| the section called “case type of instruction” |
case_type_of_when_clause |
| the section called “case type of instruction” |
type_selector | ( library_selector "." ) ? type_id | |
library_selector | library_id ( "." library_id ) * |
Example 8.7. case type of example
The following example creates a list containing items of multiple types and then counts how many objects of each type are in the list.
service instruction_examples
command case_type_of_example_1
script
// create list of objects of multiple types and add some items
const !mutable_indexed_list<any_type> untyped_list = !mutable_indexed_list_factory<any_type>.co_create
untyped_list.co_append ( "beautiful" )
untyped_list.co_append ( 123 )
untyped_list.co_append ( 123 )
untyped_list.co_append ( yes )
untyped_list.co_append ( fa_customer.co_create ( &
identifier = 123 &
name = "Sunsoft" &
city = "Suncity" ) )
untyped_list.co_append ( fa_supplier.co_create ( &
identifier = 123 &
name = "Sunsoft" ) )
// now count how many objects of each type are in the list
// initialize counters
var zero_positive32 string_count; positive32_count; customer_supplier_count; other_count = 0
var zero_positive32 positive32_total = 0
// loop over all objects in list
repeat for each any_type item in untyped_list
case type of item
when string then // if it is a string
string_count = string_count + 1 // then increment string counter
when positive32 number then // if it is a positive32 (accessible through constant 'number')
positive32_count = positive32_count + 1 // then increment positive32 counter
positive32_total = positive32_total + c_number // and update positive32_total
when customer; supplier then // is it a customer or supplier?
customer_supplier_count = customer_supplier_count + 1
otherwise // if it is none of the above types
other_count = other_count + 1 // then increment other_count
end case
end repeat
// display results
se_console.co_message ( "string : " & string_count.to_string )
se_console.co_message ( "positive32 : " & positive32_count.to_string )
se_console.co_message ( "positive32_total : " & positive32_total.to_string )
se_console.co_message ( "customer_supplier : " & customer_supplier_count.to_string )
se_console.co_message ( "others : " & other_count.to_string )
end script
end command
end serviceThe result on screen is:
string : 1 positive32 : 2 positive32_total : 246 customer_supplier : 2 others : 1