AdaCore / ada-spark-rfcs

Platform to submit RFCs for the Ada & SPARK languages
62 stars 28 forks source link

[feature] Combine .ads and .adb to one .ada file #99

Closed mhatzl closed 1 year ago

mhatzl commented 1 year ago

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

Lucretia commented 1 year ago

Ada doesn't specify it, in fact original Ada compilers took multiple compilation units from one file.

sttaft commented 1 year ago

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.

mhatzl commented 1 year ago

Thanks for your answers. I have also shared this RFC in the Ada forum, and it seems that the majority prefers separate files. As a result, I will close this issue, but thanks anyways for your insights.