Skip to main content

Basic Contents of a Class Definition

Basic Contents of a Class Definition

An InterSystems IRIS class definition can include the following items, all known as class members:

  • Methods — There are two kinds of methods: instance methods and class methods (called static methods in other languages). In most cases, a method is a subroutine.

  • Parameters — A parameter defines a constant value for use by this class. The value is set at compilation time.

  • Properties — A property contains data for an instance of the class.

  • Class queries — A class query defines an SQL query that can be used by the class and specifies a class to use as a container for the query.

  • XData blocks — An XData block is a well-formed XML document within the class, for use by the class.

  • Other kinds of class members that are relevant only for persistent classes.

InterSystems IRIS class definitions use Class Definition Language (CDL) to specify the class and its members. You can use either Python or ObjectScript to write the executable code inside of methods.

A class definition can include keywords; these affect the behavior of the class compiler. You can specify some keywords for the entire class, and others for specific class members. These keywords affect the code that the class compiler generates and thus control the behavior of the class.

The following shows a simple InterSystems IRIS class definition with methods written in ObjectScript and in Python:

Class MyApp.Main.SampleClass Extends %RegisteredObject
{

Parameter CONSTANTMESSAGE [Internal] = "Hello world!" ;

Property VariableMessage As %String [ InitialExpression = "How are you?"];

Property MessageCount As %Numeric [Required];

ClassMethod HelloWorld() As %String [ Language = objectscript ]
 {
    Set x=..#CONSTANTMESSAGE
    Return x
 }

Method WriteIt() [ Language = objectscript, ServerOnly = 1]
{
    Set count=..MessageCount
    For i=1:1:count {
        Write !,..#CONSTANTMESSAGE," ",..VariableMessage
        }
    }

}
Class MyApp.Main.SampleClass Extends %RegisteredObject
{

Parameter CONSTANTMESSAGE [Internal] = "Hello world!" ;

Property VariableMessage As %String [ InitialExpression = "How are you?"];

Property MessageCount As %Numeric [Required];

ClassMethod MessageWrapper() As %String [ Language = objectscript ]
{
     return ..#CONSTANTMESSAGE
}

ClassMethod HelloWorld() As %String [ Language = python ]
 {
    import iris
    x = iris.cls("MyApp.Main.SampleClass").MessageWrapper()
    return x
 }

Method WriteIt() [ ServerOnly = 1, Language = python ]
{
    import iris
    CONSTANTMESSAGE = self.MessageWrapper()
    count = self.MessageCount
    print()
    for i in range(count):
        print(CONSTANTMESSAGE, self.VariableMessage)
}

}

Note the following points:

  • The first line gives the name of the class. MyApp.Main.SampleClass is the full class name, MyApp.Main is the package name, and SampleClass is the short class name.

    Your IDE and other user interfaces treat each package as a folder.

  • Extends is a compiler keyword.

    The Extends keyword specifies that this class is a subclass of %RegisteredObjectOpens in a new tab, which is a system class provided for object support. This example class extends only one class, but it is possible to extend multiple other classes. Those classes, in turn, can extend other classes.

  • CONSTANTMESSAGE is a parameter. By convention, all parameters in InterSystems IRIS system classes have names in all capitals. This is a convenient convention, but you are not required to follow it.

    The Internal keyword is a compiler keyword. It marks this parameter as internal, which suppresses it from display in the class documentation. This parameter has a string value.

    You must access class parameters via ObjectScript. In the Python version of this class, we use the ObjectScript class method MessageWrapper() to return the value of the parameter.

  • You can access any class method from Python. You can use the iris.cls("Package.Class").classMethodName() syntax in all contexts, and the self.classMethodName() syntax from within a Python instance method. The example shows both syntax forms.

  • VariableMessage and MessageCount are properties. The item after As indicates the types for these properties. InitialExpression and Required are compiler keywords.

    You can access an InterSystems IRIS class property directly from ObjectScript or Python, as in the example.

  • HelloWorld() is a class method and it returns a string; this is indicated by the item after As.

    This method uses the value of the class parameter.

  • WriteIt() is an instance method and it does not return a value.

    This method uses the value of the class parameter and values of two properties.

    The ServerOnly compiler keyword means that this method will not be projected to external clients.

The following Terminal session shows how we can use this class. Both terminal shells are valid for the ObjectScript and Python versions of the class.

TESTNAMESPACE>write ##class(MyApp.Main.SampleClass).HelloWorld()
Hello world!
TESTNAMESPACE>set x=##class(MyApp.Main.SampleClass).%New()
 
TESTNAMESPACE>set x.MessageCount=3
 
TESTNAMESPACE>do x.WriteIt()
 
Hello world! How are you?
Hello world! How are you?
Hello world! How are you?
>>> print(iris.cls("MyApp.Main.SampleClass").HelloWorld())
Hello world!
>>> x=iris.cls("MyApp.Main.SampleClass")._New()
>>> x.MessageCount=3
>>> x.WriteIt()

Hello world! How are you?
Hello world! How are you?
Hello world! How are you?
FeedbackOpens in a new tab