[fpc-pascal] What's a unit?

Sven Barth pascaldragon at googlemail.com
Tue Feb 19 17:54:56 CET 2013


On 19.02.2013 17:49, Mark Morgan Lloyd wrote:
> Sven Barth wrote:
>
>>> OK, so is it possible to set up a constant extended record (i.e. the
>>> work being done entirely at compilation time) that mimics an
>>> instantiated object? Would
>>>
>>> if LibCapShim is TObject then
>>> ..
>>>
>>> be safe where LibCapShim could be either an object (possibly nil) or a
>>> constant extended record?
>>
>> You could try to overload the "is" operator for your to be defined
>> static record [AFAIK it should work]. E.g.
>>
>> === code begin ===
>>
>> TLibCapShim = record
>>   cap_get_proc: tcap_get_proc;
>>   // ...
>>
>>   class operator is (aLeft: TLibCapShim; aRight: TClass): Boolean;
>> inline;
>> end;
>>
>> class operator TLibCapShim.Is(aLeft: TLibCapShim; aRight: TClass):
>> Boolean;
>> begin
>>   Result := False;
>> end;
>>
>> === code end ===
>
> Thanks Sven, looks interesting and I'll play with it presently. Can an
> extended record's fields/methods be set up at compilation time, or do
> they need to be done by code?
>

The example I've given was for the case that you load the functions 
dynamically and use the procvars contained in the record. You could do 
it this way as well:

=== code begin ===

TLibCapShim = record
   function cap_get_proc: tcaps;
end;

//...

function TLibCapShim.cap_get_proc: tcaps;
begin
   Result := UnitContainingImports.cap_get_proc;
end;

=== code end ===

You need to pay attention to call the correct functions, of course.

Regards,
Sven



More information about the fpc-pascal mailing list