![]() |
|
||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|||||
|
|
|||||
| Related Syntax | Related Concepts | ||||
| control structure |
do select-type |
||||
Syntax
do select type record-expression as alias (case type-name local scope)* (else local-scope)? done
You can use the do select-type block to determine the type of a record at runtime.
declare record publication
field stream name
field stream publisher
declare record book
extends publication
field stream author variable
field stream year-of-publication
field stream isbn
declare record periodical
extends publication
field integer issues-per-year
field stream editor-in-chief
field stream ispn
declare record novel
extends book
field stream genre
process
local publication pubs variable
local novel war-and-peace
local periodical field-and-stream
set new pubs to war-and-peace
set new pubs to field-and-stream
repeat over pubs
do select-type pubs as p
case book
output "Book%n"
case periodical
output "Periodical%n"
case publication
output "Publication%n"
done
again
The types specified in the case statements must be the type of the expression in the select-type statement, or an extension of that type. This means that do select-type is useful only in cases where you have a shelf of a base type that may contain records of extended types and you need to determine the exact types of those records.
If the exact type of the tested expression is not specified in a case, then the closest base type will be selected. Thus the code above will print out:
Book Periodical
The type of the first record on the pubs shelf is "novel" but there is no case for "novel". Novel is extended from "book", so case book is selected. You can change the result by adding a case novel to the program.
The do select-type statement includes an alias specified by as. This alias, which must be a legal OmniMark name, can be used in the case blocks to address the specific properties of the record.
The record expression specified in the do select-type statement is a base type of the type of the specified record. You cannot use that expression to access the record fields that belong only to the extended type. The alias, by contrast, is treated as a variable of the type specified in the case statement. Thus you can query all the properties of that specific type using the alias name:
declare record point
field integer x
field integer y
declare record pixel
extends point
field stream color
declare record point-3d
extends point
field integer z
process
local pixel a
local point b variable
set a:x to 12
set a:y to 15
set a:color to "red"
set new b to a
do select-type b as pt
case pixel
output pt:color || "%n"
case point-3d
output "d" % pt:z || "%n"
case point
output "d" % pt:x || "," || "d" % pt:y
else
output "I'm stumped%n"
done
A do select-type statement may include an else clause which will be selected in case that none of the case statement match the type of the tested expression, or the types that it extends.
|
Related Syntax cast |
Related Concepts Records, extended |