Polymorphism in Ada 95 More on tagged types Classwide...

Post on 22-May-2020

4 views 0 download

transcript

Polymorphism in Ada 95 1 www.drew-hamilton.com/

Polymorphism in Ada 95

• More on tagged types • Classwide types • Dispatching

Polymorphism in Ada 95 2 www.drew-hamilton.com/

Tagged Types

•  For each tagged derivation class, there is an explicitly declared type called a classwide type.

• Mechanism for inheritance by type extension.

•  Tagged types make polymorphic programming possible.

Polymorphism in Ada 95 3 www.drew-hamilton.com/

Classwide Types

•  Each value in a classwide type consists of two parts:   a tag indicating the type of the value to

which it corresponds   the corresponding value (a value of the

type indicated by the tag. •  If T is a tagged type, then the attribute

T’Class names the classwide type for the derivation class containing T and its descendants.

Polymorphism in Ada 95 4 www.drew-hamilton.com/

Specific Types •  Unlike classwide types, explicitly declared tagged

types such as T, T1, ... are called specific types. •  If a given classwide type has values corresponding

to the values of a given specific type, then a value of the specific type can be used anywhere a value of a classwide is expected.

•  Thus a value of type T, T1, T11, T12 or T2 can be used anywhere a value of type T’Class is expected, and a value of type T1, T11 or T12 can be used anywhere a value of type T1’Class is expected.

Polymorphism in Ada 95 5 www.drew-hamilton.com/

Heterogeneous Data Structures • Classwide types can be

designated by an access type. •  Linked lists may be constructed in

which different list cells have different tags

OR •  arrays of access values that point

to objects of type T’Class with different tags.

Polymorphism in Ada 95 6 www.drew-hamilton.com/

Polymorphic Programmning

--Array of pointers to transactions: type Transaction_Array_Type is array (Positive range <>) of Transaction_Pointer_Type; --Linked list of pointers to transactions: type Transaction_List_Cell__Type; type Transaction_List_Type is access

Transaction_List_Cell_Type; type Transaction_List_Cell_Type is

record Transaction_Part : Transaction_Pointer_Type; Link_Part : Transaction_List_Type;

end record;

Polymorphism in Ada 95 7 www.drew-hamilton.com/

Manipulating Classwide Types

• Objects of a classwide type, including the objects pointed to by the Transaction_ Pointer_Type components in these data structures, can be manipulated by operations of the classwide type.

•  Such data structures provide the basis of polymorphic programming.

Polymorphism in Ada 95 8 www.drew-hamilton.com/

Subprograms for Classwide Types

•  Classwide subprograms for a type T’Class are explicitly declared with one or more parameters of type T’Class or with a result of type T’Class.

•  Dispatching subprograms for a type T’Class are the primitive subprograms of type T. Ex. a procedure declared with a formal parameter of type T can be called with an actual parameter of type T’Class.

Polymorphism in Ada 95 9 www.drew-hamilton.com/

Pointers to Objects of Classwide Types

• new T’Class’(expression) •  If declared type D is in the

derivation class of T then   new D or new D’(expression)   if such an allocator appears in a place

where a value of an access type pointing to T’Class objects is required, the effect is to allocate an object of type T’Class with the tag of D.

Polymorphism in Ada 95 10 www.drew-hamilton.com/

Classwide Subprograms

• Any subprogram with one or more parameters of a classwide type is a classwide operation.

•  In a call on a classwide operation, the actual parameter corresponding to a formal parameter of the classwide type T’Class may be of any type in the derivation class.

Polymorphism in Ada 95 11 www.drew-hamilton.com/

Function Earlier_Than

•  Re: page 530 of Cohen, Ada as a Second Language.

•  Implements a classwide operation by exploiting the fact that all values in Transaction_Type’Class have a Date_Part component.

•  Must be able to accept a Transaction_Type’Class parameter corresponding to either a Deposit_Type or a Check_Type value.

Polymorphism in Ada 95 12 www.drew-hamilton.com/

What is a Classwide Subprogram? •  A subprogram with formal parameters or a

result of a classwide type T’Class is a classwide subprogram.

•  In a call on a classwide subprogram, the actual parameters corresponding to a formal parameter of the classwide type may belong to any type in the derivation class for type T

•  These types include T’Class, T and for any type D descended from T, D’Class and D.

•  The same subprogram is invoked regardless of the tags of any classwide actual parameters.

Polymorphism in Ada 95 13 www.drew-hamilton.com/

A Dispatching Subprogram •  Is useful for operations that have entirely distinct

implementations for different types in the derivation class.

•  Is a primitive subprogram of a tagged type. •  Is declared in the same package declaration as the

tagged type, and subprogram specification names the tagged type as a parameter type, as the type pointed to by an access parameter, or as a function result type.

•  A call on such a subprogram is a dispatching call if all the formal parameters of the type have classwide or access-to-classwide actual parameters, or actual parameters that are dispatching calls.

Polymorphism in Ada 95 14 www.drew-hamilton.com/

Dispatching Calls Declarations:

type T is tagged….; A: T; procedure Classwide_Operation (X: in T’Class); procedure Dispatching_Operation (X: in T);

Calls: Classwide_Operation (A) -- classwide subprogram Classwide_Operation (T’Class(A)) --classwide subprogram Dispatching_Operation (A) --actual parameter of Type T Dispatching_Operation (T’Class(A)); -- actual parameter of

-- T’Class

Polymorphism in Ada 95 15 www.drew-hamilton.com/

Abstract types •  A type to serve as a place holder in a derived-

type hierarchy. •  A tagged type can be marked as a

placeholder type by declaring it as an abstract type.

•  A call on an abstract subprogram is required to be a dispatching call.

•  A primitive operation of a tagged type is allowed to be an abstract subprogram only if the tagged type is an abstract type.

Polymorphism in Ada 95 16 www.drew-hamilton.com/

Implications of Abstract Types

•  An abstract type is meant to be used as the parent type in a number of derived type operations.

•  Unless the derived type is declared abstract, all implicitly inherited abstract subprograms must be explicitly overridden by ordinary subprograms.

•  The compiler will enforce the design decision not to allow objects to be created for an abstract type.