In general objects may have state that may change over time during the program execution. The data-items representing the state of an object are constants and/or variables. A constant denotes the same object during the life-time of the enclosing object – the state of a constant object may, however, change over time. A variable may refer to different objects during the life-time of the enclosing object – and again, the state of these object may vary over time.
At the basic level, the state of an object is represented by the values of the constants and variable data-items. A value is a timeless concept that does not change over time. The number 7 is an example of a value.
There is thus a fundamental difference between objects that may change state and values that are timeless concepts. The support for objects and values in programming languages differ between languages. The rationale behind objects and values in Beta and qBeta is further described in the section on Objects and Values. In a program execution, values must be represented by objects or other data-structures. In qBeta, values are represented by immutable objects that are objects that does not change state over time.
The qBeta library contains a pattern Value that is a subpattern of Object. Patterns that subpattern from Value describes immutable objects.
Example
As an example of a pattern describing immutable objects, consider the following definition of Point:
Point(x: var integer, y: var integer): Value
= p1: var Point -> p2: var Point
p2 := (x = p1.x) and (y = p2.y)
move(dx,dy: var integer) -> p: var Point
p := Point(x + dx,y + dy)
Point is a subpattern of Value. Point has two arguments, x, and y that may be supplied when a Point objects is generated. In the code snippets below, the constant object a is initialized to the Point(2,3), and the variable Point b is assigned the new object Point(3,4). Both of the objects Point(2,3) and Point(3,4) cannot change state after they have been generated. Finally the assignment b := a implies that a and b now refer to the same Point object — as would be expected.
a: val Point(2,3)
b: var Point
b := Point(3,4)
b := a
Point has two local patterns (methods), pattern = that defines equality for a Point, and pattern move that generates a new Pointmoved a certain distance from the Point. Examples of use is shown below:
if (a = b) :then b := a.move(2,4)
Primitive values
The primitive values such as Integer, Char, and Real are all subpatterns of Value and thus immutable.The pattern Integer is defined in the following way:
Integer(V: var Integer):
...
+ V: var Integer -> res: var Integer
...
= V: var Integer -> res:var Boolean
...
The argument V of Integer is thus the value of the Integer object being integrated. An expression Integer(7) thus generates an immutable object representing the value 7.
x: = Integer(7)
y: var Integer
y := Integer(12)
y := x
The value x is a constant object representing the value 7. The variable y may refer to (immutable) Integer objects and may be assigned new values as in y := Integer(12), and y := x
In general numbers like 7 and 12 correspond to Integer(7) and Integer(12) respectively – the example above may thus be written as:
x: = 7
y: var Integer
y := 12
x := y
this is of course a self-recurring definition that cannot be described in qBeta without special handling of the compiler. This is also the case for other basic values such as Char and Real.
Immutable objects
The pattern Value is defined using the subpattern restriction immutable – for a precise description of immutable, see the section on Subpattern Restrictions.
Representing objects and values
In the modeling rationale associated with the Beta language theres is a fundamental distinction between objects and values.
An object was supposed to represent substance inf the form of physical material of the application domain. Values on the other hand represent measurable properties of the substance.
In many OO languages, values are objects – the value 7 is thus an object and variable having the value 7 is conceptually referring to this object. In practice this is of course not implemented this way since it would be all too inefficient. Conceptually we find it problematic to consider a value like 7 to be an object – objects have a physical location in time and space – so where is the object 7 located? This question is difficult to understand from a conceptual point of view. Especially in a distributed context with many nodes. Smalltalk was the first language to present this perspective.
In Beta, values and measurable properties are described by the enter/exit-part of an object. In qBeta values are represented by immutable objects.
Ole-Johan Dahl: “To summarize, values are timeless concepts, like the number 3, and must be represented in a computer by immutable data structures.”
The viewpojnt of Dahl is also expressed in the Beta book – values must be represented as objects or other data structures of the program execution.
The number seven is thus not a specific object somewhere in the program execution – instead several (immutable) objects may represent the value 7.
It may seem a subtle difference from the Smalltalk point of view. However, we think that conceptually there is a huge difference between considering the number 7 to be an object and immutable objects representing the value 7. In everyday life there are numerous representations of the value 7 – on paper, in Excel sheets, blackboards, etc. Their value 7 is not an object somewhere in the world.
MacLennan: Objects and values, ACM SIGPLAN Notices, is an excellent article describing the difference between objects and values.