· Mar 23, 2020

$lb ($listbuild) is instance of .... what?

I know that collection is instance of %AbstractList.

But what about $listbuild? Does it possible to detect that given variable is instance of $lb?

Discussion (4)1
Log in or sign up to continue


%SYS>w $listvalid($lb("test"))
%SYS>w $listvalid("test")     
%SYS>w $listvalid("")    

$listbuild is not an object, so, it's not a check as an instance of, it is only possible that variable is valid as $listbuild

$listbuild as just a string with a special format, so, that's why this will also return true

%SYS>w $listvalid($char(1))   
%SYS>write $lb()=$c(1)


%SYS>zzdump $lb()  

0000: 01                                                      .

$listbuild() and all other functions, like $length(), $char() etc.  are  (I call them) basic  functions of COS and have nothing to do with objects. The language of Cache (and of course IRIS )  handles data as  raw (basic) data and not as objects, like some other (but not all) languages.

For example, in JavaScript you can do

 var  text="Some text";
 alert("'" + text + "' has "+text.length + " characters");
 alert("'Some text' has " + "Some text".length + " characters");

because both, a (text)variable and a (text)string have an (object)property name 'length'.

Beside the raw data type COS also has object data type and most of the standard COS functions will accept an object property as argument, for example:

Class Test.Class Extends %RegisteredObject
Property Data as %String;

set text="Some text"
set obj = ##class(Test.Class).%New()
set obj.Data = "Some text";
write $length(text),!
write $length(obj.Data),!

And Dmitriy pointed out, $listbuild creates not an instance, but a binary string of special format.

It's a datatype %Library.List, which is one of several datatypes defined, like %Library.Integer or %Library.TimeStamp. Due to implicit conversion these datatypes are often go "invisible" in code, however, it's good to keep in mind what datatypes are you working with.

You can even create your own datatype. Some libraries/frameworks like %ZEN or Ensemble have their own library of datatypes.