typeof. To implement typeof,
each class is assigned a unique type code. This involves adding a new integer
attribute, type-of, to each class. Class constructors assign the class'
type code to this attribute when objects are created. The function
typeof() simply inspects it to determine the type of an object. The
algorithm requires only that each class have a unique type code; consequently,
multiple inheritance poses no additional difficulties. As illustrated below,
constant propagation of this attribute is one of the optimizations we hope to
enable.
![]() |
switch(typeof(this)) {
case Base:
Base::foo(); break;
case Sub1:
Sub1::foo(); break;
case Sub2:
Sub2::foo(); break;
default:
this->foo(); break;
// `this->' is optional
}
Adding a default case restricts existing optimizations, but allows incremental
program development.
As with any interprocedural optimization the quality of the optimization
improves when the entire source is available.
Once development is complete, the entire program can be compiled without
need for the default cases.