Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Programming in Scala > Chapter 19: Type Parameterization - Pg. 385

Chapter 19 Type Parameterization In this chapter, we'll explain the details of type parameterization in Scala. Along the way we'll demonstrate some of the techniques for information hiding introduced in Chapter 13 by means of a concrete example: the design of a class for purely functional queues. We're presenting type parameteri- zation and information hiding together, because information hiding can be used to obtain more general type parameterization variance annotations. Type parameterization allows you to write generic classes and traits. For example, sets are generic and take a type parameter: they are defined as Set[T] . As a result, any particular set instance might be a Set[String] , a Set[Int] , etc.--but it must be a set of something. Unlike Java, which allows raw types, Scala requires that you specify type parameters. Variance defines inheritance relationships of parameterized types, such as whether a Set[String] , for example, is a subtype of Set[AnyRef] . The chapter contains three parts. The first part develops a data struc- ture for purely functional queues. The second part develops techniques to hide internal representation details of this structure. The final part explains variance of type parameters and how it interacts with information hiding. 19.1 Functional queues head tail enqueue A functional queue is a data structure with three operations: returns the first element of the queue returns a queue without its first element returns a new queue with a given element appended at the end 385