· Nov 13, 2023

How to get type of a property

I want to get a persistent object property type in objectscript and I don't find how to do this.

Can someone help me ?

Product version: IRIS 2023.1
Discussion (5)2
Log in or sign up to continue

You should remember that the types defined in the formal argument list of a Method declaration or defined when a local variable uses the #dim preprocessor directive are only advisory types.  Any ObjectScipt value or oref can passed as an actual arugment to a method.  A local variable specified in a #dim directive can contain any ObjectScript value or oref--not just the type/class specified in the #dim directive.

The type of a Property name is only checked when the %Save method (or other appropriate method) is applied to an object containing that Property.  While the object is active in memory you can assign any ObjectScript value or oref to that property.  You can choose to write you own specific PropertySet/PropertyGet methods containing code that applies your choice of run-time type checking and conversions during the modifications/evaluations of Property names.

The ObjectScript language has type-less variable contents and the types in the Class language extensions to ObjectScript are only enforced by run-time executed methods.

But of course, the Classes in the %Dictionary Package are useful for writing run-time methods that enforce the declared types of Properties.

Thanks Julius,

It was exactly what i needed. My goal was to copy properties from a dynamic object to a persistent object, but in dynamic object for reference, I have only id, not a ref.

So i made this : (main class)

Class Bna.Utils.DynToPersistent Extends %RegisteredObject

Property DynObject As %Library.DynamicObject;

Property PersistentObject;

Method %OnNew(dynObject As %Library.DynamicObject, persistentObject) As %Status
    Set ..DynObject = dynObject
    Set ..PersistentObject = persistentObject
    Return $$$OK

Method copyDynToPersistent()
    set pClass = ..PersistentObject.%ClassName(1)
    Set iterator = ..DynObject.%GetIterator()
    while iterator.%GetNext(.pProp, .pValue) {
        Set pType = ##class(Bna.Utils.Common).GetPersistentObjectPropertyType(pClass,pProp)
        Set isRef = ##class(Bna.Utils.Common).PersistentObjectPropertyTypeIsReference(pType)
        if isRef {
            set value = $ZOBJCLASSMETHOD(pType, "%OpenId", pValue)
        } else {
            set value = pValue
        Set $ZOBJPROPERTY(..PersistentObject, pProp) = value


(utility methods) :

ClassMethod GetPersistentObjectPropertyType(pClass As %String, pKey As %String) As %String
	set def=##class(%Dictionary.PropertyDefinition).%OpenId(pClass_"||"_pKey)
	if def Return def.Type

ClassMethod PersistentObjectPropertyTypeIsReference(pType) As %Boolean
	if $EXTRACT(pType,1,1) = "%" { Return 0 }
	else { Return 1 }

 I think my test for reference detection can be light, but enough in my context