当前位置: 动力学知识库 > 问答 > 编程问答 >

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 instanciate 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, mosty 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 do 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.

网友答案:

This question is about Scala but I think it's worth mentioning that the <: [type operatator?] is not unique to Scala and instead originates in Type Theory; see for example the article about Subtyping on Wikipedia which makes extensive use of this operator.

In fact, due to its strong connections with type theory <: is not the only thing Scala (elegantly) borrowed from it; for example the term: Type notation (seen in e.g. val foo: Foo, def fact(x: Int): Int) also comes from Type Theory.

分享给朋友:
您可能感兴趣的文章:
随机阅读: