Podstrony
- Strona startowa
- Bruce Morris Adventure Guide Florida Keys & Everglades National Park (2005)
- Larock Bruce E Hydraulics of Pipeline Systems
- Professional Feature Writing Bruce Garrison(3)
- Professional Feature Writing Bruce Garrison (2)
- Professional Feature Writing Bruce Garrison
- Professional Feature Writing Bruce Garrison(6)
- Simmons Dan Hyperion
- McDevitt Jack Brzegi zapomnianego morza
- Robert Ludlum Mozaika Persifala
- Fermenty t. 1 i 2 Reymont W
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- plazow.keep.pl
[ Pobierz całość w formacie PDF ]
.This is especially the case withbinary methods, where one often wishes to guarantee that the receiver andparameter of the method are of the exact same type.For example, it takesonly a little thought to realize it would be disastrous to try to build a linkedlist where some of the nodes are singly linked and others are doubly linked.This is one reason why it is important that doubly linked nodes are not al-lowed to be defined as subclasses of singly linked nodes in languages wheresubclasses always result in subtypes.In order to provide the programmer with the capability to decide whetheror not a type declaration should restrict an identifier to represent only a sin-gle type or a range of types, we introduce the following notations in.We write18.2 Introducing hash types 353x:Tto indicate that the value ofxmust be of typeT, but not a subtype ofT.Wewritex:#Tto indicate that the value ofxmust be of some typeT such thatT T.HASH TYPE We call types of the form#T, hash types.Because matching is only definedon object types, only object types (including type variables representing ob-ject types) can be used in constructing hash types.For example, one may notuse hash types with function types.Here is a simple example of the use of hash types.Suppose we wish todefine a node class (similar to that in Figure 16.3) that holds items from anyobject type matchingString.Then we could declare the instance variable:value:#StringBecause declaring a variable or formal parameter to have a hash typeTal-lows it to hold values of all types matchingT, items with hash types are usedin a very similar way as regular variables or formal parameters in languageswith subtyping.While hash types add expressiveness to , there are some restric-tions on their use that are necessary to preserve type safety.We illustrate thepossible problems with a familiar example.Recall the definitions ofNodeTypeandDoubleNodeTypein Figure 16.5.WhileDoubleNodeType NodeType, they are not subtypes.The classesNodeandDoubleNodegenerate objects of those types.In anticipation of the discussion below, we repeat methodsetNextfromDoubleNodebelow:function setNext(newNext:MyType): Void is{super.setNext(newNext);newNext setPrevious(close(self))}Recall that methodsetPreviouswas introduced inDoubleNodeTypebutwas not contained inNodeType.Suppose thatn1,n2:#NodeType.Then their values could either be of typeNodeTypeor of typeDoubleNodeType(or potentially any other extensionofNodeType).Suppose we wished to attachn2as the next element fromn1.That is, we would like to write the statement:354 18 Simplifying: Dropping Subtyping for Matchingn1 setNext(n2)While this seems quite plausible, there are problems.Suppose that at runtime,n1is actually an object of typeDoubleNodeType, whilen2is of typeNodeType.Ifn1is generated by classDoubleNode, execution of the abovecode will result in a type error.In particular, execution of the code forset-NextinDoubleNoderesults in sending the messagesetPreviouston2,an object of typeNodeTypethat does not have a method with that name.This is essentially the same argument we gave back in Section 4.2 to showthat extensions of object types with methods having parameters of typeMy-Type(or in Eiffel s case,like Current), did not result in subtypes.Wemust prevent this method invocation from being legal if we wish to have astatically type-safe language.If we think more carefully about what is going on, we can see that theproblem is thatsetNextrequires a parameter that has exactly typeMyType.However, because we do not know the exact type ofn1, we cannot know attype-checking time whatMyTypeis supposed to represent!It might seem that changing the signature ofsetNextso that it takes aparameter of type#MyTypemight help, but changing that and the type of theinstance variablenextto#MyTypedo not help, as the same counterexampleshows this is not type safe.As before, in order to understand whether aparameter tosetNextis legal, we must know the meaning ofMyTypeatthe time of type checking.In this example, we definitely do not know themeaning ofMyTypeif all that we know is that the type ofn1is#NodeType.There is no difficulty in determining what is legal if we know the exacttype of the receiver,n1, eveninthe case where we make the parameter typeofsetNextand the type of instance variablenextbe#MyType.1 Supposethe parameter type is#MyType.Ifn1:NodeTypethen actual parameters forn1 setnextof type#NodeType,NodeType, andDoubleNodeTypeallwork without type problems.Ifn1has type (exactly)DoubleNodeType,then actual parameters ofsetNextwith type#DoubleNodeTypeorDou-bleNodeTypeboth work without difficulty.Of course, the use of an actualparameter of typeNodeTypewill not be type safe.The use of an actual pa-rameter of type#NodeTypealso should not type check because it may notbe safe.1.Actually, while there are no type problems, we will find the class is not very usable if thetype ofnextand the associated method parameters and return types are declared to have type#MyType.If we get a node as a result of sendinggetNextto another node, we lose the exacttype and hence can t sendsetNextmessages to it.18.2 Introducing hash types 355As a general rule, we will find the use ofMyTypevery handy wheneverwe have a homogeneous collection of values to be dealt with.However,MyTypeis rarely appropriate when we have a heterogeneous collection ofvalues.ThusMyTypeis very handy in the definition of singly or doublylinked nodes, because lists are composed of either homogeneous collectionsof singly linked nodes or homogeneous collections of doubly linked nodes.2We can define a class for binary search trees as follows:class BinarySearchTree(T OrderableMT) {function insert(T): Void is.;function find(T): Boolean is.;function remove(T): Boolean is
[ Pobierz całość w formacie PDF ]