#dim
Description
This macro preprocessor directive specifies the intended data type of a local variable and can optionally specify its initial value. #dim is provided as an optional convenience for documenting code. ObjectScript is a typeless language; it does not declare a data type for a variable, nor does it enforce the data typing specified in #dim. The #dim directive has any of the following forms:
#dim VariableName As DataType
#dim VariableName As DataType = InitialValue
#dim VariableName As List Of DataType
#dim VariableName As Array Of DataType
where:
-
VariableName is the variable being defined, or a comma-separated list of variables.
-
DataType is the type of VariableName, specifically a class name.
-
InitialValue is a value optionally specified for VariableName. This is equivalent to SET VariableName=InitialValue. (This syntax is not available for lists or arrays.)
DataType is principally for use with IDEs, which can provide code-completion assistance.
InitialValue
-
If VariableName specifies a comma-separated list of data variables, and DataType is a data type class, all the variables receive the same value. For example:
#dim d,e,f As %String = "abcdef"
This is equivalent to:
SET d = "abcdef" SET e = "abcdef" SET f = "abcdef"
-
If VariableName specifies a comma-separated list of object variables and DataType is an object class, each variable is assigned a separate OREF. For example, the following assigns separate OREFs to each variable:
#dim j,k,l As Sample.Person = ##class(Sample.Person).%New()
This is equivalent to:
SET j = ##class(Sample.Person).%New() SET k = ##class(Sample.Person).%New() SET l = ##class(Sample.Person).%New()
However, if you omit the As DataType qualification (not a common use case), all variables receive the same OREF:
#dim j,k,l = ##class(Sample.Person).%New()
The result here is equivalent to:
SET j = ##class(Sample.Person).%New() SET k = j SET l = j
-
If VariableName specifies a comma-separated list of variables and DataType is a Dynamic object class, each variable is assigned a separate OREF, as in the case of ordinary object classes. For example, the following assigns separate OREFs to each variable:
#dim m,n,o As %DynamicObject = {"name":"Fred"}
The same is true for this:
#dim p,q,r As %DynamicArray = ["element1","element2"]
However, if you omit the As DataType qualification (not a common use case), all variables receive the same OREF, as in the case of ordinary object classes.