[fpc-pascal] What's a unit?

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

Does a unit- rather than something it contains- have any sort of 
representation which is recognisably distinct from an object?

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

caps := LibCapShim.cap_get_proc;  // LibCapShim is object or unit
if caps <> nil then
     caplist := capability;
     r := LibCapShim.cap_set_flag(caps, flag, 1, @caplist, Ord(value));

which is OK at the application level irrespective of how the program's 

What I can't do, without a conditional directive, is check whether the 
object representing the library is nil (because the dll/so isn't 
available). Instead I'm having to do

   if LibCapShim = nil then      (* Initialisation failed        *)
{$endif                    }
   caps := LibCapShim.cap_get_proc;

and so on. There's obviously ways around this, but they tend to rely on 
adding validity functions outside the object. Is there any way of doing 
something like

if LibCapShim is TObject then
   if LibCapShim = nil then      (* Initialisation failed        *)

without the compiler objecting when it finds itself looking at a unit 
name in isolation?

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