Objects and member variables

Posted: January 11th, 2010 | Author: Mars | Filed under: Uncategorized | Comments Off

An object contains a group of related things: pieces of data and functions which operate on the data. We can think of an object as a kind of associative container, then, mapping names to members. What is the value of bar, one may ask; or how do I perform the baz operation?

In many object-oriented languages, this mapping is established by a class. Radian objects are simpler; instead of combining instantiation, inheritance, and type identification into one structure, each of these functions is implemented separately. The object itself, once you’ve constructed it, is just a function which returns a value in response to some key, which is the name of the function or variable you want to retrieve.

The dot-syntax for members is therefore a specialized form of function call. If you have an object foo, and you want its member bar, you would write this:
baz = foo.bar
This operation performs two calls. First, you call the object foo, passing in the symbol bar. The object responds with a reference to whichever function it is that can return the value of bar. Then you call that function, passing in the object reference, and it returns the value.

The lookup mechanism and the accessor mechanism are thus separated from one another. Accessors do not need to have any privileged relationship with the object, provided they are capable of doing the job requested of them; any function which returns an appropriate value when presented with the object can work. Further, it doesn’t matter where the data actually comes from; it may live in the object, it may live somewhere else, or it may be computed on demand, but the mechanism for retrieving it is the same in each case.

The object constructor syntax is effectively a dictionary, or map, constructor. The object block itself represents a function which returns some object; the object is an associative mapping of keys, which are member names, to values, which are functions that return some interesting information. Each new item declared in the object block adds such a mapping to the resulting object.

Each function or method defined inside this block gets an implicit parameter, self, representing the object it is expected to work on. This need not be declared explicitly, as in Python; it is a byproduct of the object-constructor context. Variables defined inside an object block also receive special handling: they are marked in the symbol table as self-members, so that the compiler can generate code to look the value up through the “self” object, or to assign a new value by assigning to the “self” object.


Comments are closed.