What does "<:" mean in Scala?

Viewed 35268

I'm looking at p. 469 of "Programming in Scala" Second Edition. There is a line of code that reads:

type Currency <: AbstractCurrency

I cannot decipher what this means.

4 Answers

I want to add some points which will describe the usability benefits of the <: notation.

Let us say, you define the following class for your API:

case class myApiClass[param <: BaseParameter](requestBody: String, parameter: param)

You have a trait called BaseParameter

trait BaseParameter

Then, you have the following parameters:

case object Parameter1 extends BaseParameter
case object Parameter2 extends BaseParameter
case object Parameter3

Now, whenever you create a myApiClass instance, you must pass an object as the argument "parameter", whose class/which itself implements BaseParameter (e.g. Parameter1 and Parameter2). Concretely, this is an assertion, and would not work if you pass Parameter3.

Related