My favorites | Sign in
ect
Project Home Downloads Wiki Issues Source
Search
for
ClassDataStructure  
Underlying data structure of objects
Featured
Updated Aug 25, 2009 by fehe...@gmail.com

Introduction

An ECT object only stores data about its type and values of its fields. All these data are stored in a record: in each object, the following fields are present:

  • _types: a field for additional type information (first field)
  • fields of superclasses of the object (in order of inheritance chain)
  • fields of the class of the object (last fields)

Examples

Class animal

-class(animal).
?FIELDS({weight, color = red}).

The corresponding record definition for this, as generated in animal.class.hrl:

-record(animal,{'_types' = {animal},
               weight,
               color = red}).

The result of expression #animal{} is:

{animal, {animal}, undefined, red}

We can see that weight has the value undefined, because it is not initialized.

Class giraffe

-class(giraffe).
?SUPERCLASS(animal).
?FIELDS({height = 5, giraffe_id}).

The corresponding record definition for this, as generated in giraffe.class.hrl:

-record(giraffe,{'_types' = {animal,giraffe},
                 weight,
                 color = red,
                 height = 5,
                 giraffe_id}).

We can see that fields by extension are added at the end of the record. (It is always done this way.)

The result of expression #giraffe{} is:

{giraffe, {animal, giraffe}, undefined, red, 5, undefined}

Field _types is a tuple of the superclasses and class of the object, enumerated in order of inheritance. It makes possible to check an object if it has a particular class, in O(1) time. This type-check is done each time the fields of an object are accessed.


Sign in to add a comment
Powered by Google Project Hosting