|
Literal objectsOther objects can also be constructed as literals. There are three kinds of slot- containing objects:
Plain objects are constructed by including a list of slot descriptions in a slot list, delimited by vertical bars, which in turn is enclosed in parentheses:
(| x. y. z |) This object contains three assignable data slots: x, y, z (all initialized to nil). A method resembles a plain object except that code follows the slot list. When the method is evaluated, the slots play the role of local variables. The value returned by the method is the value of the last expression. We will look at method invocation semantics in more detail later.
(| x. y | x: 3. y: 4. x squared + y squared ) A constant slot can be created by supplying an initial value after an equals sign:
(| x = 3. y = 4 | x squared + y squared ) An assignable slot can also be initialized, using a left arrow:
(| x <- 3. y <- 3 | y: y + 1. x squared + y squared ) All the above forms of slot declaration can be used in plain, method and block objects. In the initializing forms, the initializing expression is evaluated at compile- time in the context of the lobby. Finally, a parent slot can be created using any of the above forms, by suffixing the name of the slot with an asterisk (*). The example shows the initial definition of a prototypical 2-d point, to illustrate the various kinds of literal object declarations.
|