What is the REAL content of $THIS (because it seems, $THIS is not always the expected $THIS)?
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>
Comments
Looks like a bug. Please check with WRC.
Simplified your example a bit:
Class DC.ValueOfThis Extends %RegisteredObject
{
/// do ##class(DC.ValueOfThis).Test()
ClassMethod Test()
{
write $zv,!!
set obj=..%New()
do obj.Work()
write $$$FormatText("classmethod: $this %1, ..Value() %2", $this, ..Value()),!
do obj.Work()
}
Method Work()
{
write $$$FormatText("method: $this %1, ..Value() %2", $this, ..Value()),!
}
ClassMethod Value()
{
quit $this
}
}
Are you asking this out of curiosity? Or is there another reason for the question? How would you like to use $this?
I had a real use case, where from an instance method a class method was called and that class method used $this as argument in a subsequent call. In the very first call the content of $this was the class name (as documented) but in subsequent calls it contained the OREF and that caused a <CLASS DOES NOT EXIST> error. A WRC ticket is already open.
Right now the WRC told me, the problem is corrected