[fpc-pascal] What's a unit?
印場 乃亜
shiruba at galapagossoftware.com
Sun Mar 10 06:32:29 CET 2013
Hi,
On 2013/02/19, at 22:53, Marco van de Voort <marcov at stack.nl> 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 never though of it that way, but certainly that's true in the sense that if you want multiple objects, you can instantiate them at run time. With a unit, you only get one. In a sense, then, a unit is like a class with only class methods.
In fact, even without having used those words - when I design a program for something where I know I will only need to track the state of a single item, I will often use units. As soon as it's likely I will need to track the state of multiple items (particularly an unknown number of items), I have to think about using arrays i the unit, or just converting to objects.
>
>> 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
Sadly this kind of thing has been one of the drawbacks of existing pascal implementations. For example, you have program A which uses unit B.
You want to make B dynamic, so you convert it into a library (.so or .dll). But you still want to use it in your program. Back when I did this, at least, that required writing another shim unit which pulled in everything in the library. It's understandable that you need the shim if the original program was written in C, etc., but a bit silly in the case where both were pascal. Making full use of dynamic linking has been a pain in general. (I prefer static linking most of the time, but there are advantages and disadvantages to both). Your case isn't a lot different - but a unit can't do everything an object can anyway (as discussed above).
Probably the easiest thing for you to do is have the unit and the library both include a file with the object code. That's not pretty, but it's better than double maintenance I suppose.
>
> 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.
Thank you,
Noah Silva
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
More information about the fpc-pascal
mailing list