SIMPOL Documentation

Complex Object Types

The object types that are also value types, such as string or boolean, exist primarily to hold values of that type. More complicated types exist either to provide information to the program, or to allow the program to do something. For example the fsfileinputstream type is used to read data from a file in a file system (such as on disk or over a network). With the simple value types an object is created for a local variable to refer to when an assignment is made to that local variable, as in the string s;s = 'Hello world' example earlier. With non-value types this is not the case — it is necessary to explicitly create these objects, normally using a new() function. The following example creates an input stream to read from the file c:\autoexec.bat:

function main()
  fsfileinputstream f

  f =@ fsfileinputstream.new("c:\autoexec.bat")
end function

The =@ operator in this example is an important one which caused the local variable f to be set to refer to the object on the right hand side of the operator. This should be contrasted with the = operator which instructs the value on the right hand side to be assigned to the object referred to by the local variable on the left. As a rather pedantic yet instructive example, the following program has a resulting value of 1:

function main()
  integer i
  integer j

  i = 1
  j = i
  i = 3
end function j

whereas the following program produces a result of 3:

function main()
  integer i
  integer j

  i = 1
  j =@ i
  i = 3
end function j

The difference is that j = i assigns to the integer object referred to by j the value of the integer object referred to by i, whereas j =@ i causes both i and j to refer to the same integer object, so when a value is assigned to i it is setting the value of the object to which j refers.

The input stream is destroyed (closing the file) when the local variable f is destroyed, at the end of the function. In the case of an input stream, the parameters that have to be passed to a new function depend on the type of object being created, in this case only the filename is required.

Object types also have properties, which are either embedded objects of other types or references to other objects. For example the fsfileinputstream type has a property called filename of type string, which contains the name of the file being read. Extending the previous example slightly gives a program the result of which is the value 'c:\autoexec.bat':

function main()
  fsfileinputstream f

  f =@ fsfileinputstream.new("c:\autoexec.bat")
end function f.filename

Object types can also have member functions, or methods, which are functions that do something with or to the object. For example the fsfileinputstream type has a member function called getstring, which can be used to read a string from the input file. The following program has a return value that is the first line of the c:\autoexec.bat file. The exact syntax for member functions such as getstring is type and function dependent, and can be found in the language reference.

function main()
  fsfileinputstream f

  f =@ fsfileinputstream.new("c:\autoexec.bat")
end function f.getstring(.inf,1,.true,.char(13)+.char(10))