Search code examples
importincludepascal

Is the "uses" statement in Pascal equivalent to "#include" in C++ or "import" in Java?


From what I know, in C++ applications, if you include a header file - the contents of the header file are pasted into the code.

On the other hand, in Java - when you import a Java library - I think the parameters are passed to the library in the JVM.

How does the behaviour of the uses clause in Pascal programs compare to this?


Solution

  • All three have to do with how to construct a program from multiple source files.

    C/C++ has the rock bottom level, all required declarations the compiler need to be included and compiled again for each piece of source (.c,.cpp) as if it was one big source.

    Pascal also supports this (it has a $INCLUDE directive), but it is much less used (and even then more in system programming, and hardly at all in application programming). Using this inherits all the problems C/C++ have, but since the use is magnitudes less this is less visible.

    This because with the Uses/units system Pascal has a better system that can import other pieces safely and automatically without recompiling them. It is also possible to inline functions without declaring them in the header.

    Java pretty much does the same in principle, but its implementation is totally different (based on hierarchical nested namespaces instead of namespaces that are always of the form unitname.identifiername[.fieldnames] like Pascal)

    Both Java and Pascal gain though that the compiler can compile a program by only passing the main file and a bunch of paths. The compiler will the research itself what to compile and what not. It also enables them to automatically construct a linker line from the found pieces of source.

    This renders the need for complex external buildsystems (like make) and manually maintaining administration (makefiles) of this for basic building moot.

    So in summary, they are all about the same (interactions between multiple source files), but the Pascal and Java systems build on top of that for ease of use, and to give more control at the compiler

    Added later: The core principle of modular systems is to hard link one header to each main code file (which is only linked by convention in C), and to make changes to the preprocessor local to only that file (and $INCLUDE'd, but not USES'd/imported files. So the preprocessor state at the start of a module is only dependent on the compiler startup state. (read: internal compiler and commandline/environment). Note that this means it is not possible to import macro's via USES, since they are preprocessor state.