Eduard Lebedyuk · Aug 31, 2017

What's the fastest way to check if one class is a subclass of another

I need to check if one class is a subclass of another (either direct or indirect).

For example:

Class Package.ClassA Extends %Library.Persistent

Class Package.ClassB Extends Package.ClassA 

Class Package.ClassC Extends Package.ClassB

In this example Package.ClassC is a subclass of 3 classes: %Library.Persistent, Package.ClassA, Package.ClassB.

So any of these checks should return 1:

Write ##class(Some.System.Class).IsSubclass("Package.ClassC", "%Library.Persistent")
Write ##class(Some.System.Class).IsSubclass("Package.ClassC", " Package.ClassA")
Write ##class(Some.System.Class).IsSubclass("Package.ClassC", " Package.ClassB")
0 588
Discussion (3)0
Log in or sign up to continue
write $classmethod(subclass,"%IsA",superclass)


I've always used "%Extends" for this purpose:

Write $classmethod(subclass,"%Extends",superclass)

Both methods scan the entire inheritance tree, but in the case of multiple inheritance, "%IsA" only finds primary superclasses, whereas "%Extends" finds all superclasses.

For example, consider the following class:

Class Ens.BusinessService Extends (Ens.Host, Ens.Util.IOLogger)

And the corresponding output:

ENSEMBLE>zw $classmethod("Ens.BusinessService","%IsA","Ens.Util.IOLogger")
ENSEMBLE>zw $classmethod("Ens.BusinessService","%Extends","Ens.Util.IOLogger")

More information about the difference between the two methods is discussed here.