官术网_书友最值得收藏!

  • Learn Scala Programming
  • Slava Schmidt
  • 338字
  • 2021-06-10 19:35:46

Generalized type constraints

In the previous two sections, we used type constraints provided by the language to precisely define type members and type parameters. There are also supplementary generalized type constraints defined in the standard library that allow you to define relations between types using type classes. We will look at type classes and implicits in detail in Chapter 4, Getting to Know Implicits and Type Classes, but we will give a brief introduction to generalized type constraints here.

The <:< constraint expresses the requirement that the left-side type is a subtype of the right-side type. Basically, having an instance of  A <:< B is the same as having a definition of A <: B. But why is it needed, then? Because sometimes the language is not expressive enough. Let's look at an example:

abstract class Wrapper[A] {
val a: A
// A in flatten shadows A in the Wrapper
// def flatten[B, A <: Wrapper[B]]: Wrapper[B] = a
def flatten(implicit ev: A <:< Wrapper[B]): Wrapper[B] = a
}

It is not possible to express the A <: Wrapper[B] type constraint because the A in this definition will shadow the A type constraint in the definition of Wrapper[A]. The implicit, ev, solves this problem easily. ev will be available in scope if the compiler can prove that the subtype relation holds.

Another generalized type constraint available in the Scala standard library is =:=. So, A =:= B allows you to require that A and B are equal, the same way that A <:< B allows you to express subtyping relation. Due to restrictions on subclassing, it also witnesses that A <:< B, but not that B <:< A. We will take a look in detail how this equality relation can be used to express domain constraints at the end of this chapter.

The strange syntax of A <:< B and A =:= B brings us to the next section, Infix types.

主站蜘蛛池模板: 西昌市| 天全县| 永济市| 汕头市| 东兰县| 海安县| 岳普湖县| 玉田县| 康乐县| 始兴县| 陆川县| 丽江市| 芜湖县| 柞水县| 舞钢市| 军事| 西华县| 申扎县| 马鞍山市| 永安市| 梓潼县| 城市| 铜鼓县| 灯塔市| 四会市| 镶黄旗| 伊春市| 安溪县| 张家口市| 美姑县| 宁晋县| 若尔盖县| 遂宁市| 瓦房店市| 辽源市| 桃园县| 望谟县| 拜泉县| 酒泉市| 阳城县| 山阴县|