Skip to main content

#dim

Specifies the intended data type of a local variable and can optionally specify its initial value.

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. It has the form:

#dim VariableName As DataTypeName
#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. Specifying a data type is optional, you can omit As DataType and just specify =InitialValue.

  • 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 code-completion assistance.

InitialValue

  • If VariableName specifies a comma-separated list of data variables and DataType is omitted, all the variables are assigned the same initial value. For example:

    #dim a,b,c = ##class(Sample.Person).%New()

    This is equivalent to:

      SET (a,b,c) = ##class(Sample.Person).%New()

    which assigns the same OREF to each variable.

  • If VariableName specifies a comma-separated list of data variables, and DataType is a standard %Library data type, all the variables are assigned the same data type and initial value. For example:

    #dim d,e,f As %String = ##class(Sample.Person).%New()

    This is equivalent to:

      SET (d,e,f) = ##class(Sample.Person).%New()

    which assigns the same OREF to each variable.

  • If VariableName specifies a comma-separated list of object variables and DataType not a %Library data type, each variable is assigned a separate OREF. For example:

    #dim j,k,l As Sample.Person = ##class(Sample.Person).%New()

    assign separate OREFs to each variable.

  • If VariableName specifies a comma-separated list of variables and DataType is a Dynamic data type, each variable is assigned a separate OREF. For example:

    #dim m,n,o As %DynamicObject = {"name":"Fred"}
    #dim p,q,r As %DynamicArray = ["element1","element2"]

    assign separate OREFs to each variable.

FeedbackOpens in a new tab