Not sure if there is single built in method that will return the type of any property but there are functions to check if a property is object $ISOBJECT or is valid number $ISVALIDNUM etc . Refer https://docs.intersystems.com/iris20211/csp/docbook/DocBook.UI.Page.cls?... for list of available functions you could use.
/// Use the propertydefinition-class:/// Open your.classname || propertyname/// For example:set def=##class(%Dictionary.PropertyDefinition).%OpenId("Sample.Person||Name")
if def write def.Type
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.
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
}
}
}
Not sure if there is single built in method that will return the type of any property but there are functions to check if a property is object $ISOBJECT or is valid number $ISVALIDNUM etc .
Refer https://docs.intersystems.com/iris20211/csp/docbook/DocBook.UI.Page.cls?... for list of available functions you could use.
Hope that help.
Thanks George,
It's not what i search, I would have the type, not test if is one type.
/// Use the propertydefinition-class: /// Open your.classname || propertyname /// For example: set def=##class(%Dictionary.PropertyDefinition).%OpenId("Sample.Person||Name") if def write def.Type
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