Question
· Jul 13, 2021

Method return List of type

Good afternoon I am making a method of a class that I want to return a list of objects of another class that I have defined, example:

ClassMethod myMethod() As list of MyPackage.MyClass
{
}

But it gives me a syntax error, I try this and it works:

ClassMethod myMethod() As %ListOfObjects
{
}

But I would like to keep the typing.

Is there a way?

Product version: Caché 2018.1
Discussion (5)2
Log in or sign up to continue

ObjectScript variables are untyped, so preserving the type isn't necessary.

You'll build the list by adding your MyPackage.MyClass objects to it, then you'll return the %ListOfObjects, then you'll use that list's methods to manipulate those objects. For instance, set mything = mylist.GetAt(1) will give you an object that is identical to the MyPackage.MyClass object you put in the list with all of its properties an methods.

The right way to describe a list of a particular type of object is:

ClassMethod myMethod() As %ListOfObjects(ELEMENTTYPE="MyPackage.MyClass")

This is most relevant from the perspective of XML projections - e.g., web services. You can also define a subclass of %ListOfObjects with the ELEMENTTYPE parameter overridden for reuse. See https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=GX...