There are two kinds of methods: instance methods and class methods (called static methods in other languages).
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.