# Examples 8.1.2.5:
CAST

The features of the required library class CAST are used in individual examples. None of the examples given is a complete class nor necessarily a 'complete' method.

In all of the examples it will be assumed that the code shown is in a class called, say, EXAMPLE.

#### create

This feature creates a new object with the desired argument type for use in 'one-line' cast operations. It could be written in the EXAMPLE class as follows -

caster : CAST{CARD} := CAST{CARD}::create ;

#### from

This is the routine which provides the 'one-line' type case facility. Although this is not strictly essential to the Sather language, it is provided in the library to enhance program readability. For example, given the above object creation -

number : $NUMBER := 42 ; -- which makes it a cardinal number!
val : CARD := caster.from(number) ;

which is equivalent to the rather longer version

val : CARD ;
number : $NUMBER := 42 ; -- which makes it a cardinal number!
typecase number
when CARD then
val := number
else
raise number
end

which is essentially how an implementer may choose to implement the from routine.

| Specification Index |  | Language Index |  | Section 8 Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Thursday, 25 May 2000. |