4.7. Stubs

Example 4-9. Example:

stub register_object(ob:FOO);
stub abstract_signature

A stub feature may only be present in a partial class. They have no body and are used to reserve a signature for redefinition by an including class. If code in a partial class contains calls to an unimplemented method, that method must be explicitly provided as a stub

4.7.1. Code inclusion examples.

Example 4-10. The class 'ARRAY{T}' in the standard library is not a primitive data type. It is based on a built-in class 'AREF{T}' which provides objects with an array portion. 'ARRAY' obtains this functionality using an 'include', but chooses to modify the visibility of some of the methods. It also defines additional methods such a 'contains', 'sort', etc. The methods 'aget', 'aset' and 'asize' are defined as 'private' in 'AREF', but 'ARRAY' redefines them to be public.

class ARRAY{T} is
   private include AREF{T}
      -- Make these public.
      aget->aget,
      aset->aset,
      asize->asize;
   ...
   contains(e:T):BOOL is ... end
   ...
end

Example 4-11. This code demonstrates the use of partial classes. Each MIXIN class provides a different way of prompting the user; each can be combined with COMPUTE to make a complete program. The stub in COMPUTE allows that class to be type checked without needing either mix-in class.

Only COMPUTE_A and COMPUTE_B may actually be instantiated.

This style of code reuse is very flexible because the stub routines can access private data in COMPUTE. Such flexibility requires extra care, because it bypasses ordinary class encapsulation.

partial class MIXIN_A is
   prompt_user is ... end;
end;

partial class MIXIN_B is
   prompt_user is ... end;
end;

partial class COMPUTE is
   main is ...prompt_user; ... end;
   stub prompt_user;
end;

class COMPUTE_A is
   include COMPUTE;
   include MIXIN_A;
end;

class COMPUTE_B is
   include COMPUTE;
   include MIXIN_B;
end;