Article directory
Chapter 5 ObjectScript
Both methods and routines can be ObjectScript
written using , but most modern code is written using methods. Methods are contained in classes, which allows similar methods to be grouped together, automatic generation of documentation in the class reference, and the use IRIS
of object-oriented features of .
This doesn't mean routines
it's not important. Many useful system utilities are written as routines, and routines are generated when a class is compiled.
Example class
Shown below is a User.DemoClass
sample class named containing ObjectScript
methods written in . This example gives us a chance to look at some common ObjectScript
commands, operators, and functions, and see how code is organized within methods.
Class User.DemoClass
{
/// Generate a random number.
/// This method can be called from outside the class.
ClassMethod Random() [ Language = objectscript ]
{
set rand=$RANDOM(10)+1 ; rand is an integer in the range 1-10
write "Your random number: "_rand
set name=..GetNumberName(rand)
write !, "Name of this number: "_name
}
/// Input a number.
/// This method can be called from outside the class.
ClassMethod Input() [ Language = objectscript ]
{
read "Enter a number from 1 to 10: ", input
set name=..GetNumberName(input)
write !, "Name of this number: "_name
}
/// Given an number, return the name.
/// This method can be called only from within this class.
ClassMethod GetNumberName(number As %Integer) As %Integer [ Language = objectscript, Private ]
{
set name=$CASE(number,1:"one",2:"two",3:"three",
4:"four",5:"five",6:"six",7:"seven",8:"eight",
9:"nine",10:"ten",:"other")
quit name
}
/// Write some interesting values.
/// This method can be called from outside the class.
ClassMethod Interesting() [ Language = objectscript ]
{
write "Today's date: "_$ZDATE($HOROLOG,3)
write !,"Your installed version: "_$ZVERSION
write !,"Your username: "_$USERNAME
write !,"Your security roles: "_$ROLES
}
}
Please note the following points:
Random()
andInput()
the method callGetNumberName()
method, which is a private method of this class and cannot be called from outside the class.WRITE
,QUIT
,SET
andREAD
are commands. The language also includes other commands for deleting variables, for controlling program flow, for controllingI/O
devices, for managing transactions (possibly nested), and so on.
Command names are not case-sensitive, although by convention they appear in all uppercase in run text.
- This example includes two operators. The plus sign (
+
) performs addition, and the underscore (_
) performs string concatenation.
ObjectScript
Provides commonly used operators and some special operators not seen in other languages.
$RANDOM
,$CASE
and$ZDATE
are functions.
The language provides functions for string operations, various conversions, formatting operations, mathematical operations, etc.
$HOROLOG
,$ZVERSION
,$USERNAME
and$ROLES
are system variables (IRIS
called special variables in ). Most special variables containIRIS
values for the operating environment, current processing status, etc.ObjectScript
Comment lines, block comments, and comments at the end of statements are supported.
We can execute the methods of this class in the terminal as a demonstration. In these examples, <TESTNAMESPACE>
it's the prompt displayed in the terminal. The text following the prompt on the same line is the command entered. The lines after that show the values that the system writes to the terminal in response.
TESTNAMESPACE>do ##class(User.DemoClass).Input()
Enter a number from 1 to 10: 7
Name of this number: seven
TESTNAMESPACE>do ##class(User.DemoClass).Interesting()
Today's date: 2021-07-15
Your installed version: IRIS for Windows (x86-64) 2019.3 (Build 310U) Mon Oct 21 2019 13:48:58 EDT
Your username: SuperUser
Your security roles: %All
TESTNAMESPACE>