Skip to main content

Variable Typing and Conversion

Variable Typing and Conversion

Variables in ObjectScript are untyped — there are no specified data types. (This is also true of JavaScript, VBScript, and Document Data Base/JSON.) This means that you can assign a string value to a variable and, later on, assign a numeric value to the same variable. As an optimization, InterSystems IRIS may use different internal representations for strings, integers, numbers, and objects, but this is not visible to the application programmer. InterSystems IRIS automatically converts (or interprets) the value of a variable based on the context in which it is used.

Some examples:

 // set some variables
 SET a = "This is a string"
 SET b = "3 little pigs"
 SET int = 22
 SET num = 2.2
 SET obj = ##class(Sample.Person).%New()

 // Display them
 WRITE "Here are the variables themselves: ",!
 WRITE "a: ",a,!
 WRITE "b: ",b,!
 WRITE "int: ",int,!
 WRITE "num: ",num,!
 WRITE "obj: ",obj,!,!

 // Now use them as other "types"
 WRITE "Here are the numeric interpretation of",!
 WRITE "a, b, and obj: ",!
 WRITE "+a: ",+a,!
 WRITE "+b: ",+b,!
 WRITE "+obj: ",+obj,!,!
 
 WRITE "Here are concatenations of int and num:",!
 WRITE "Concatenating int: ","I found " _ int _ " apples.",!
 WRITE "Concatenating num: ","There are " _ num _ " pounds per kilogram.",!

InterSystems IRIS converts values as follows:

ObjectScript Type Conversion Rules
From To Rules
Number String A string of characters that represents the numeric value is used, such as 2.2 for the variable num in the previous example.
String Number Leading characters of the string are interpreted as a numeric literal, as described in String-to-Number Conversion. For example, “–1.20abc” is interpreted as -1.2 and “abc123” is interpreted as 0.
Object Number The internal object instance number of the given object reference is used. The value is an integer.
Object String A string of the form n@cls is used, where n is the internal object instance number and cls is the class name of the given object.
Number Object Not allowed.
String Object Not allowed.
FeedbackOpens in a new tab