· Oct 12, 2023 1m read

How to pass changeable number of parameters to a method

InterSystems FAQ rubric

ObjectScript allows you to pass any number of arguments using arrays. Do it by adding ... after the argument name.

An example is as follows. In the example statement, the argument information is set in a global variable (a variable stored in the database) so that it can be easily checked after the method is executed.

Class TEST.ARGTEST1 Extends %RegisteredObject
ClassMethod NewMethod1(Arg... As %StringAs %Boolean
 kill ^a
 merge ^a = Arg

The result of running it in the terminal is as follows.

USER>DO ##class(TEST.ARGTEST1).NewMethod1(1,2,3,4,5)
Discussion (1)1
Log in or sign up to continue

Thanks Mihoko!

Also note: the argument with ... after its name doesn't have to be the only argument to the method; it must be the last argument to the method. So NewMethod1()'s signature could be:

ClassMethod NewMethod1(a as %String, b as %String, c... as %String)
    kill ^a, ^b, ^c
    set ^a = a, ^b = b
    merge ^c = c

Running it like this:

USER>DO ##class(TEST.ARGTEST1).NewMethod1(1,2,3,4,5,6,7)
USER>ZWRITE ^a, ^b, ^c