[fpc-pascal] What's a unit?

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Tue Feb 19 15:18:14 CET 2013

Marco van de Voort wrote:
> In our previous episode, Mark Morgan Lloyd said:
>> Does a unit- rather than something it contains- have any sort of 
>> representation which is recognisably distinct from an object?
> Maybe. But what it certain doesn't have is a runtime instance. unit is
> mostly a compiletime concept, and the Delphi exceptions to that (mostly due to
> package system) are not implemented.
>> I've got a situation where if a library (.dll or .so) is opened under 
>> program control it is represented by an object, with entry points 
>> expressed as methods. Alternatively, if it's statically linked then it's 
>> represented by a unit, with entry points represented by procedures and 
>> functions. That allows me to write things like
> No, that does not exist to my best knowledge. Delphi might have ways
> for packages, but that is also shaky (and target dependent) at best. 
> The only way is to define a record with procedure variables for the static
> case manually I guess. 

I admit that I felt fairly pleased with myself when I hit upon using 
units/objects like this, but this is definitely one of the downsides.

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?

As I said, there's ways around it and I'm not entirely averse to a 
conditional directive. But I'm interested in whether there's an elegant 
solution- even if I can't use it immediately due to compiler versions etc.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the fpc-pascal mailing list