[Grace-core] Semantics of object/classes

Kim Bruce kim at cs.pomona.edu
Tue Aug 7 12:50:12 PDT 2012


Responding to Andrew's challenge, here is my first crack at describing the (informal) semantics of objects, classes, and inheritance.  Note that I did not specify the semantics of inheriting from an object as I am not sure which semantics we want -- one based on the initial definition of the object, or one reflecting the dynamic state.

Semantics of objects:

An object is a collection of constants (from “defs”), variables (from “var” declarations), and methods.  (We treat inner class definitions as constants which represent closures, so don’t worry about them further.) 

When an object is generated from either a class or an object expression, space is allocated for all of the features and then the initialization code for it is executed from beginning to end.  Because methods are represented as closures, their bodies are not executed until there is an appropriate method request.  Definitions and variable declarations can only be referred to after their definitions/declarations, but methods may be called from anywhere in the executable code.  If a definition or variable is referred to before it is initialized then it will cause a run-time error.

When a class inherits from another class, the new class has all of the features of the original class as well as all of those added in the new definition.  If a method overrides a method in the superclass, then the new method replaces the old one.  When an object is generated from a subclass, space for all of the features (old and new) is allocated, and then the initialization code is run.  The initialization code from the superclass is run first, followed by the code added in the subclass.  When executing the initialization code (whether old or new), the identifier “self” refers to the object under construction.  Method requests of the form super.m occurring in the new start-up code or in new methods are statically bound to the code of the corresponding method of the superclass.  The interpretation of self in that code is still that of the newly created object, not an object from the superclass.

When an object inherits from a class, the definition is similar, except that the object is created during the evaluation of the definition, rather than upon generation from a class.

 -----------------------

I'm sure that further clarifications will be necessary, but thought I would share this first crack at the problem.  Please let me know what is not clear.

Kim



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailhost.cecs.pdx.edu/mailman/private/grace-core/attachments/20120807/2e7e8436/attachment.html>


More information about the Grace-core mailing list