Article
· Jul 6, 2023 1m read

How to overload methods in ObjectScript

InterSystems FAQ rubric

The InterSystems ObjectScript language does not allow you to define methods of the same name with different arguments. It is generally classified as a programming language called a dynamic language.

In ObjectScript, you can freely control which arguments are used when executing a method, so unlike languages ​​such as Java, which are not dynamic programming languages, there is no need to strictly distinguish methods by the number of arguments at the compilation stage.

The ObjectScript language, therefore, does not contain a language specification commonly called overloading.

To implement a function equivalent to overloading when porting a program written in Java, etc., add ... after the last argument like

ClassMethod test(args... as %String)

This allows passing variable length arguments.

If multiple arguments are passed to this method, they will be set sequentially, such as args(1) = first argument, args(2) = second argument, and so on.

You can use this to get the number of arguments passed in the method code and branch the processing.

*However, the data type of the argument cannot be determined.

Discussion (0)1
Log in or sign up to continue