Skip to Content
Programming Scala, 3rd Edition
book

Programming Scala, 3rd Edition

by Dean Wampler
May 2021
Intermediate to advanced
553 pages
13h 45m
English
O'Reilly Media, Inc.
Book available
Content preview from Programming Scala, 3rd Edition

Chapter 17. Scala’s Type System, Part II

This chapter continues the type system survey that we started in the previous chapter, covering more advanced constructs. You can skip this chapter until you need to understand the concepts discussed here.

Let’s begin with match types and the broad subject of dependent typing.

Match Types

Scala 3 extends pattern matching to work at the type level for match types. Let’s look at an example adapted from the Scala documentation. The following match type definition returns a type that is the type parameter of another type with one type parameter. For example, for Seq[Int] it will return Int:

// src/script/scala/progscala3/typesystem/matchtypes/MatchTypes.scala

type Elem[X] = X match                                          1
  case String => Char                                           2
  case IterableOnce[t] => t                                     3
  case Array[t] => t
  case ? => X                                                   4
1

Define a match type. It uses a match expression with case clauses to resolve the type.

Special case handling of Strings, which are actually Array[Char].

scala.collection.IterableOnce is ...

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.
Start your free trial

You might also like

Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

Dean Wampler, Alex Payne
Functional Programming in Scala, Second Edition

Functional Programming in Scala, Second Edition

Paul Chiusano, Runar Bjarnason, Michael Pilquist
The Go Programming Language

The Go Programming Language

Alan A. A. Donovan, Brian W. Kernighan

Publisher Resources

ISBN: 9781492077886Errata PageSupplemental Content