Question
· 11 hr ago

What is the REAL content of $THIS (because it seems, $THIS is not always the expected $THIS)?

According to documentation, quotation: "$THIS contains the current class context.
The class context for an instance method is the current object reference (OREF).
The class context for a class method is the current classname as a string value."
 
As my example below shows, either the documentation or the implementation (or both) is wrong, I always call a class method (Value) and expected the class name as the return value but got either the class name or an OREF. Moreover, if I repeat the call, I get another values. But why?
Does anyone have a clear explanation (for an aging brain) or have I misunderstood something?

Class DC.ValueOfThis Extends %RegisteredObject
{

ClassMethod Test()
{
	write $zv,!!
	set obj=..%New()
	do obj.Work()
	write "From classmethod: ",$this," ",$this," ",..Value()," ",..Value()," ",..Value()," ",$this,!
	do obj.Work()
}

Method Work()
{
	write "From inst.method: ",$this," ",$this," ",..Value()," ",..Value()," ",..Value()," ",$this,!
}

ClassMethod Value()
{
	quit $this
}

}

And the test output is:

USER>

USER>d ##class(DC.ValueOfThis).Test()
IRIS for UNIX (Ubuntu Server LTS for x86-64) 2021.2 (Build 649U) Thu Jan 20 2022 08:49:51 EST

From inst.method: 1@DC.ValueOfThis 1@DC.ValueOfThis DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis
From classmethod: DC.ValueOfThis DC.ValueOfThis DC.ValueOfThis DC.ValueOfThis DC.ValueOfThis DC.ValueOfThis
From inst.method: 1@DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis 1@DC.ValueOfThis

USER>
Product version: IRIS 2021.2
Discussion (0)1
Log in or sign up to continue