What does "<:" mean in Scala?
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.
It means an abstract type member is defined (inside some context, e.g. a trait or class), so that concrete implementations of that context must define that type. However, there is a constraint that this type (Currency
) must actually be a subtype of AbstractCurrency
. That way the abstract context can operate with Currency
, knowing that it understands every operation of AbstractCurrency
.
trait AbstractCurrency {
def disappearInGreece(): Unit
}
abstract class Economy {
type Currency <: AbstractCurrency
def curr: Currency
// can call disappear... because `Currency`
// is an `AbstractCurrency`
def shake(): Unit = curr.disappearInGreece()
}
Trying to define Currency
without constraints:
trait RadioactiveBeef
class NiceTry(val curr: RadioactiveBeef) extends Economy {
type Currency = RadioactiveBeef
}
Fails. With constraints ok:
trait Euro extends AbstractCurrency
class Angela(val curr: Euro) extends Economy {
type Currency = Euro
}
It means "must be a subtype of", "must conforms to", "must extends". Most of the time, it would appear as a bound on a generic parameter, such as
class Home[P <: Person]
Each Home is fit for a certain type of person, a Home[Person]
accepts any person, there might be Home[Student]
, Home[Elderly]
, but no Home[Planet]
.
type Currency <: AbstractCurrency
introduces an abstract type
member Currency
in the class
/trait
where it appears. Descendants will have to choose a type so that they can be concrete. The <: AbstractCurrencies force them to choose a subtype of AbstractCurrency
(including AbstractCurrency
, which is allowed).
An abstract type member is very close to a type parameter, just as an abstract value member is close to a constructor parameter.
If you have class A(val x: X){...}
, you instanciate the first with new A(myX)
. If you have class A{val x: X; ...}
, you instanciate with new A{val x = myX }
.
If you have class Market[Currency <: AbstractCurrency]
you instanciate the type with Market[SomeCurrencyType]
. If you have Market{type Currency <: AbstractCurrency}
, you instantiate with Market{type Currency = SomeCurrencyType}
. However, Market
is a valid type. It means you don't know what type of currency this market uses (which may restrict how you can use it).
Using an abstract type member rather than a type parameter may have benefits, mostly if the type parameter does not appear in the public interface of the type, if Market
has no Currency
appearing as a function parameter or result (not too likely on this example). Then client does not need to write Market[SomeCurrencyType]
, Market
will do. Of course, the CurrencyType
will have to be known when a market is created, but then it may be passed along simply as Market
.