Download Free Pascal Language Reference Guide

Transcript
CHAPTER 8. GENERICS
end.
and a program
program myb;
uses mya;
procedure DoLocalThings;
begin
Writeln(’myb.DoLocalThings’);
end;
Type
TB = specialize TMyClass<Integer>;
Var
B : TB;
begin
B:=TB.Create;
B.DoSomething(1);
end.
Despite the fact that generics act as a macro which is replayed at specialization time, the
reference to DoLocalThings is resolved when TMyClass is defined, not when TB is defined. This means that the output of the program is:
home: >fpc -S2 myb.pp
home: >myb
mya.DoLocalThings
This is dictated by safety and necessity:
1. A programmer specializing a class has no way of knowing which local procedures are
used, so he cannot accidentally ’override’ it.
2. A programmer specializing a class has no way of knowing which local procedures are
used, so he cannot implement it either, since he does not know the parameters.
3. If implementation procedures are used as in the example above, they cannot be referenced from outside the unit. They could be in another unit altogether, and the
programmer has no way of knowing he should include them before specializing his
class.
93