Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Extension Methods

Exercise 1

val cities = List("London", "Paris", "Lisbon", "Berlin")

Create a list of cities:

  1. In an object create an extension method print to the String class.
  2. In another object create a second extension method print to the String that prints in ALL-CAPS
  3. Choice which print method you will use with an import

Create a case class CapitalCity with a city and a country
Create a list of capital cities

  1. Create an extension method print to the CapitalCity companion object
  2. Print the list of capital cities

Exercise 2

Create a case class Person with a name and age
Create a list of persons

  1. Create an extension method print to the Person companion object.
  2. In another object create an extension method print to the Persion that prints in ALL-CAPS
  3. Choice which print method you will use with an import