Skip to main content

Methods

Methods

There are two kinds of methods: instance methods and class methods (called static methods in other languages).

Specifying Method Keywords

In a method definition, you can include optional compiler keywords that affect how the method behaves. The following list shows some of the most commonly seen method keywords:

In InterSystems IRIS, methods can be written in ObjectScript or Python. To specify which language you will write a method in, use the following syntax:

Method MyMethod() [ Language = objectscript ]
{
    // implementation details written in ObjectScript
}
Method MyMethod() [ Language = python ]
{
    # implementation details written in Python
}

If a method does not use the Language keyword the compiler will assume that the method is written in ObjectScript.

You must write the method’s language in all lowercase letters, as in the example.

This keyword specifies that the method is private and can only be used with ObjectScript methods. Subclasses inherit the value of the Private keyword and cannot override it.

By default, methods are public and can be accessed anywhere. You can mark a method as private (via the Private keyword). If you do:

  • It can only be accessed by methods of the class to which it belongs.

  • It does not appear in the InterSystems Class Reference.

It is, however, inherited and available in subclasses of the class that defines the method.

Other languages often call such methods protected methods.

References to Other Class Members

Within a method, use the syntax shown here to refer to other class members:

  • To refer to a parameter, use an expression like this:

     ..#PARAMETERNAME
    
    # technique 1
    iris.cls("Package.Class")._GetParameter("PARAMETERNAME")
    
    # technique 2
    objectinstance._GetParameter("PARAMETERNAME")
    

    In classes provided by InterSystems, all parameters are defined in all capitals, by convention, but your code is not required to do this.

  • To refer to another instance method, use an expression like this:

     ..methodname(arguments)
    
    self.methodname(arguments)
    

    Note that you cannot use this syntax within a class method to refer to an instance method.

  • To refer to another class method, use the following syntax:

     ..classmethodname(arguments)
    
    # technique 1
    iris.cls("Package.Class").classmethodname(arguments)
    
    # technique 2
    iris.cls(__name__).classmethodname(arguments)
    

    Note that you cannot use the Python self syntax to access a class method. Instead, you can use the __name__ property to obtain the name of the current class, as shown in the above example.

  • (Within an instance method only) To refer to a property of the instance, use an expression like this:

     ..PropertyName
    
    self.PropertyName
    
    

    Similarly, to refer to a property of an object-valued property, use an expression like this:

     ..PropertyNameA.PropertyNameB
    
    self.PropertyNameA.PropertyNameB
    

    The syntax used in the ObjectScript examples is known as dot syntax.

    Also, you can invoke an instance method or class method of an object-valued property. For example:

     Do ..PropertyName.MyMethod()
    self.PropertyName.MyMethod()
    

References to Methods of Other Classes

Within a method (or within a routine), use the syntax shown here to refer to a method in some other class:

  • To invoke a class method and access its return value, use an expression like the following:

     ##class(Package.Class).MethodName(arguments)
    
    iris.cls("Package.Class").MethodName(arguments)
    
    

    For example:

     Set x=##class(Util.Utils).GetToday()
    x=iris.cls("Util.Utils").GetToday()
    

    You can also invoke a class method without accessing its return value as follows:

     Do ##class(Util.Utils).DumpValues()
    iris.cls("Util.Utils").DumpValues()
    
    Note:

    ##class is not case-sensitive.

  • To invoke an instance method, create an instance and then use an expression like the following in either ObjectScript or Python to invoke the method and access its return value:

    instance.MethodName(arguments)
    

    For example:

     Set x=instance.GetName()
    x=instance.GetName()
    

    You can also invoke an instance method without accessing its return value by calling the method as follows:

     Do instance.InsertItem("abc")
    instance.InsertItem("abc")
    

Not all methods have return values, so choose the syntax appropriate for your case.

References to Current Instance

Within an instance method, sometimes it is necessary to refer to the current instance itself, rather than to a property or method of the instance. For example, you might need to pass the current instance as an argument when invoking some other code.

In ObjectScript, use the special variable $THIS to refer to the current instance. In Python, use the variable self to refer to the current instance.

For example:

 Set sc=header.ProcessService($this)
sc=header.ProcessService(self)

Method Arguments

A method can take positional arguments in a comma-separated list. For each argument, you can specify a type and the default value.

For instance, here is the partial definition of a method that takes three arguments. This is valid syntax for both ObjectScript and Python methods within InterSystems IRIS classes:

Method Calculate(count As %Integer, name, state As %String = "CA") as %Numeric
{
    // ...
}

Notice that two of the arguments have explicit types, and one has an default value. Generally it is a good idea to explicitly specify the type of each argument.

Note:

If a method is defined in Python and has any arguments with default values, then these arguments must be at the end of the argument list to avoid a compilation error.

Skipping Arguments

When invoking a method you can skip arguments if there are suitable defaults for them. ObjectScript and Python each have their own syntax to skip arguments.

In ObjectScript, you can skip over an argument by providing no value for that argument and maintaining the comma structure. For example, the following is valid:

 set myval=##class(mypackage.myclass).GetValue(,,,,,,4)

In an InterSystems IRIS class, a Python method’s signature must list the required arguments first, followed by any arguments with default values.

When calling the method, you must provide arguments in the order of the method’s signature. Therefore, once you skip an argument you must also skip all arguments following it. For example, the following is valid:

ClassMethod Skip(a1, a2 As %Integer = 2, a3 As %Integer = 3) [ Language = python ]
{
    print(a1, a2, a3)
}
TESTNAMESPACE>do ##class(mypackage.myclass).Skip(1)
1 2 3

Passing Variables by Value or by Reference

When you invoke a method, you can pass values of variables to that method either by value or by reference.

The signature of a method usually indicates whether you are intending to pass arguments by reference. For example:

Method MyMethod(argument1, ByRef argument2, Output argument3)

The ByRef keyword indicates that you should pass this argument by reference. The Output keyword indicates that you should pass this argument by reference and that the method ignores any value that you initially give to this argument.

Similarly, when you define a method, you use the ByRef and Output keywords in the method signature to inform other users of the method how it is meant to be used.

To pass an argument by reference in ObjectScript, place a period before the variable name when invoking the method. In Python, use iris.ref() on the value you want to pass and call the method on the reference. Both of these are shown in the following example:

 Do MyMethod(arg1, .arg2, .arg3)
arg2=iris.ref("peanut butter")
arg3=iris.ref("jelly")
MyMethod(arg1,arg2,arg3)
Important:

The ByRef and Output keywords provide information for the benefit of anyone using the InterSystems Class Reference. They do not affect the behavior of the code. It is the responsibility of the writer of the method to enforce any rules about how the method is to be invoked.

Variable Numbers of Arguments

You can define a method so that it accepts a variable number of arguments. For example:

ClassMethod MultiArg(Arg1... As %List) [ Language = objectscript ]
{
 Set args = $GET(Arg1, 0)
 Write "Invocation has ",
     args,
     " element",
     $SELECT((args=1):"", 1:"s"), !
 For i = 1 : 1 : args
 {
     Write "Argument[", i , "]: ", $GET(Arg1(i), "<NULL>"), !
 }
}
ClassMethod MultiArg(Arg1... As %List) [ Language = Python ]
{
    print("Invocation has", len(Arg1), "elements")
    for i in range(len(Arg1)):
        print("Argument[" + str(i+1) + "]: " + Arg1[i])
}

Specifying Default Values

To specify an argument’s default value in either an ObjectScript or a Python method, use the syntax as shown in the following example:

Method Test(flag As %Integer = 0)
{
 //method details
}

When a method is invoked, it uses its default values (if specified) for any missing arguments. If a method is written in Python, then any arguments with default values must be defined at the end of the argument list.

In ObjectScript, another option is to use the $GET function to set a default value. For example:

Method Test(flag As %Integer)
{
  set flag=$GET(flag,0)
 //method details
}

This technique, however, does not affect the class signature.

FeedbackOpens in a new tab