In this section... 8.1.1 Application-Independent Naming 8.1.2 Abbreviations 8.1.3 Generic Formal Parameters |
Summary of Guidelines from this section |
------------------------------------------------------------------------ generic type Item is limited private; ... package Bounded_Stack is procedure Push (New_Item : in Item); procedure Pop (Newest_Item : in Item); ... end Bounded_Stack; ------------------------------------------------------------------------ |
Renamed appropriately for use in current application:
------------------------------------------------------------------------ with Bounded_Stack; package Cafeteria is type Tray is limited private; package Tray_Stack is new Bounded_Stack (Item => Tray , ...); ... end Cafeteria; ------------------------------------------------------------------------ |
When there is an obvious choice for the simplest, clearest name for a reusable
part, it is a good idea to leave that name for use by the reuser of the part,
choosing a longer, more descriptive name for the reusable part. Thus,
Bounded_Stack
is a better name than Stack
for a generic stack package because
it leaves the simpler name Stack
available to be used by an instantiation.
Include indications of the behavioral characteristics (but not indications of the implementation) in the name of a reusable part so that multiple parts with the same abstraction (e.g., multiple stack packages) but with different restrictions (bounded, unbounded, etc.) can be stored in the same Ada library and used as part of the same Ada program.
Language Ref Manual references: 2.3 Identifiers, 4.1 Universal Expressions, 12.1 Generic Declarations, 12.3 Generic Instantiation
------------------------------------------------------------------------ with Calendar; package Greenwich_Mean_Time is function Clock return Calendar.Time; ... end Greenwich_Mean_Time; ------------------------------------------------------------------------ |
But the following abbreviation may not be clear when used in an application.
with Calendar; with Greenwich_Mean_Time; ... function Get_GMT return Calendar.Time renames Greenwich_Mean_Time.Clock; |
The difference between this guideline and Guideline 3.1.4 involves issues of domain. When the domain is well-defined, abbreviations and acronyms that are accepted in that domain will clarify the meaning of the application. When that same code is removed from its domain-specific context, those abbreviations may become meaningless.
In the example above, the package, Greenwich_Mean_Time, could be used in any application without loss of meaning. But the function Get_GMT could easily be confused with some other acronym in a different domain.
renames
clause. If a with Greenwich_Mean_Time; ... package GMT renames Greenwich_Mean_Time; |
Language Ref Manual references: 2.3 Identifiers, 4.1 Universal Expressions, 12.1 Generic Declarations
------------------------------------------------------------------------ generic -- Index provides access to values in a structure. For example, -- an array, A. type Index is (<>); -- The function, Less_Than, does NOT compare the indexes -- themselves;it compares the elements of the structure: -- Less_Than (i,j) <==> A(i) < A(j) with function Less_Than (Index1 : in Index; Index2 : in Index) return Boolean; -- This procedure swaps values of the structure (the mode won't -- allow the indexes themselves to be swapped!): -- Less_Than (i,j) and then Swap (i,j) ==> Less_Than (j,i). with procedure Swap (Index1 : in Index; Index2 : in Index); -- After the call to Quick_Sort, the indexed structure will be -- sorted: -- For all i,j in First..Last : i<j => A(i) < A(j). procedure Quick_Sort (First : in Index := Index'First; Last : in Index := Index'Last); ------------------------------------------------------------------------ |
In a sense, a generic specification is a contract where the instantiator must supply the formal parameters and in return receives a working instance of the specification. Both parties are best served when the contract is complete and clear about all assumptions.
Language Ref Manual references: 12.1 Generic Declarations