问题
From the compiler's point of view, what's the difference between declaring an Ada type in a package spec or doing it inside the body?
回答1:
Generally it is a good practice to make declarations (of types, but also other items like constants or subprograms) the most local possible. In your case if the type is used only in the body and not for users of your package specification (even as private type), put it in the body. Furthermore, if it is used only in a subprogram of the body, put it in that subprogram. It reduces the odds of name clashes and makes things clearer. To summarize, the advantages for the compiler (smaller identifier sets at given points in the code) are the same as for the programmer.
回答2:
Ada's strong support for encapsulation includes modular programming in the form of semantically coherent packages. From the compiler's point of view, "the separation of a package's specification from its body…can reduce compilation time." Focusing on the abstract data type examined in the topic on privacy, it's clear that the public part is what clients can access. In contrast, the private part is accessible to the implementation. It includes sufficient implementation detail for the compiler to generate code for an arbitrary client, but no more. Once an altered package body is successfully compiled, existing clients can rely on the unchanged specification, without the need to recompile the corresponding body.
来源:https://stackoverflow.com/questions/62985072/ada-encapsulation-and-private-types