Closed mhatzl closed 1 year ago
Ada doesn't specify it, in fact original Ada compilers took multiple compilation units from one file.
The AdaMulti compiler from Green Hills and the ObjectAda compiler from PTC both support combining multiple units into a single file. GNAT supports this as well, by using the gnatname utility.
Be that as it may, in my view separating specifications from bodies has many advantages, both from a separate compilation point of view, and from a methodological point of view. There are cases where it is not as useful to make the separation, and in fact the "expression_function" feature of Ada is useful in exactly those situations. But in many other cases, the "audience" for a spec and the "audience" for a body are quite different, and combining them into a single file can be distracting for both audiences.
Summary
Modern languages typically have the definition and implementation parts in one file. Therefore, I propose
.ada
as extension for files in which the body part of a procedure/function is implemented where it is declared. The.ada
file may be seen as an.adb
file that allows usage before declaration.Motivation
With declaration and implementation split up, it is cumbersome to make changes to the definition. Changing the name or type of the procedure, function, or parameter in one file must also be copied to the other.
Documenting is at the moment also suboptimal, because you mostly write documentation above/below the definition. So you either have to copy and synchronise your documentation with the implementation, or accept the missing documentation in the implementation. Without the documentation above/below the implementation, I often need to check the declaration on what this procedure/function should do exactly.
The separation also makes formal verification more tedious, because pre- and postconditions are set in the definition. These conditions are not copied to the implementation, where it would be helpful to know what the constraints are. This leads to frequently jumping between
.ads
and.adb
files..ada
files would also keep declaration and implementation close together. I have seen some repositories that have a separate "implementations" folder. This makes synchronising the definition and implementation even worse.Finally, I think
.ada
is a good extension for Ada language files. Having a new extension also prevents backward compatibility issues.Caveats and alternatives
Compiler complexity
I know the reason why languages like C and Ada had initially split declaration and implementation was compiler complexity. Since the compiler at least needs to parse everything twice to discover procedures, functions, etc. that are declared after being used.
With modern hardware, this is not a problem anymore.
Handling of privates
Everything defined in the
private
section of an.ads
file may be accessed in the implementation.This is still possible in
.ada
files, because the implementation is clearly separable from the declaration. So everything declared below theprivate
marker is only accessible in the implementation parts. It also has the added bonus that it would be possible to use privates in pre-/postconditions. Which is not possible at the moment (as far as I know). Also the workaround with declaring private types at the top of.ads
files is not needed anymore.Packages only used in the implementation
At the moment declaration and implementation are split up, and packages only used in the implementation may not be specified in the
.ads
file.With the combination to one
.ada
file, this is not possible anymore. However, I do not see a real benefit of splitting with's for declaration and implementation.Documentation placement
I have seen some repositories that have the documentation below the declaration. From what I know, the reason is "better" readability of the declaration itself.
With the combination, having the documentation below is probably not what most want.
This leaves two options: