From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c0d3b96261238735,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1993-03-18 08:14:42 PST Newsgroups: comp.lang.ada Path: sparky!uunet!inmet!spock!stt From: stt@spock.camb.inmet.com (Tucker Taft) Subject: Type, Class, and Package in Ada 9X Message-ID: <1993Mar17.201026.13787@inmet.camb.inmet.com> Sender: news@inmet.camb.inmet.com Nntp-Posting-Host: spock Organization: Intermetrics Inc, Cambridge MA Distribution: comp.lang.ada Date: Wed, 17 Mar 1993 20:10:26 GMT Date: 1993-03-17T20:10:26+00:00 List-Id: Someone recently asked me to explain the difference between the meaning of the term "class" in C++ and its meaning in Ada 9X. Here is a synopsis of the answer: In C++, the term "class" refers to three different, but related things: a) a language construct, that encapsulates the definitions of data members, member functions, nested types, etc.; b) a particular kind of type, defined by a class construct (or by "struct" which is a special case of "class"); c) a set of types consisting of a type and all of its derivatives, direct and indirect. In Ada 9X, the term "class" refers only to the third of the above definitions. Ada 9X (and Ada 83) has three different terms for the concepts corresponding to the above three things: a) a "package" encapsulates the definitions of types, objects, operations, exceptions, etc which are logically related. (The operations of a type defined immediately within the package where the type is declared are called, in 9X, the "primitive operations" of the type, and in some sense, define the "primitive" semantics of the type, especially if it is a private type.) b) a "type" is characterized by a set of values and a set of primitive operations (there are a million definitions of "type," unfortunately, but you know what I mean...); c) a "class" is a set of types with similar values and operations; in particular, a type and and all of its derivatives, direct and indirect, represents a (derivation) class. Also, the set of integer types form the integer "class," and so on for the other language-defined classes of types in the language. Some OOP languages take an intermediary position. In CLOS, a "class" is not an encapsulating construct (CLOS has "packages"). However, a "class" is both a type and a set of types, depending on context. (Methods "float" freely.) The distinction Ada 9X makes between types and classes (= set of types) carries over into the semantic model, and allows some interesting capabilities not present in C++. In particular, in Ada 9X one can declare a "class-wide" object initialized by copy from a "class-wide" formal parameter, with the new object carrying over the underlying type of the actual parameter. For example: procedure Print_In_Bold(X : T'Class) is -- Copy X, make it bold face, and then print it. Copy_Of_X : T'Class := X; begin Make_Bold(Copy_Of_X); Print(Copy_Of_X); end P; In C++, when you declare an object, you must specify the "exact" class of the object -- it cannot be determined by the underlying class of the initializing value. Implementing the above procedure in a general way in C++ would be slightly more tedious. Similarly, in Ada 9X one can define an access type that designates only one specific type, or alternatively, one can define one that can designate objects of any type in a class (a "class-wide" access type). For example: type Fancy_Window_Ptr is access Fancy_Window; -- Only points at Fancy Windows -- no derivatives allowed type Any_Window_Ptr is access Window'Class; -- Points at Windows, and any derivatives thereof. In C++, all pointers/references are "class-wide" in this sense; you can't restrict them to point at only one "specific" type. In other words, C++ makes the distinction between "specific" and "class-wide" based on pointer/reference versus object/value, whereas in Ada 9X, this distinction is explicit, and corresponds to the distinction between "type" (one specific type) and "class" (set of types). The Ada 9X approach we believe (hope ;-) gives somewhat better control over static versus dynamic binding, and is less error prone since it is type-based, rather than being based on reference vs. value. In any case, in Ada 9X, C++, and CLOS it makes sense to talk about "class libraries," since a given library will generally consist of a set of interrelated types. In Ada 9X and CLOS, one could alternatively talk about a set of "reusable packages" and mean essentially the same thing. S. Tucker Taft Ada 9X Mapping/Revision Team Intermetrics, Inc. Cambridge, MA 02138