Chapter 15. Scala’s Type System, Part II
This chapter continues the survey of the type system that we started in the previous chapter. The type features discussed here are the ones you’ll encounter eventually, but you don’t need to understand them right away if you’re new to Scala. As you work on Scala projects and use third-party libraries, if you encounter a type system concept that you haven’t seen before, you’ll probably find it covered here. (For more depth than we can cover here, see The Scala Language Specification.) Still, I recommend you skim the chapter. For example, you’ll see a few examples of path-dependent types in more advanced examples later in the book, although you won’t need a “deep” understanding of them.
Path-Dependent Types
Scala, like Java before it, lets you nest types. You can access nested types using a path expression.
Consider the following example:
// src/main/scala/progscala2/typesystem/typepaths/type-path.scalaXpackageprogscala2.typesystem.typepathsclassService{classLogger{deflog(message:String):Unit=println(s"log:$message")}vallogger:Logger=newLogger}vals1=newServicevals2=newService{overridevallogger=s1.logger}// ERROR!
Define a class
Servicewith a nested classLogger.Use
printlnfor simplicitly. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access