第45讲:Scala中Context Bounds代码实战及其在Spark中的应用源码解析
与view bounds一样context bounds(上下文界定)也是隐式参数的语法糖我们使用view bounds的方式的写法如下:
class Pairs[T =0) first else second
}
} 如果我们利用隐式转换,可以改成如下写法
class Pairs_implicit(first: T,second:T){
def bigger(implicit ordered: Ordering) ={
if (ordered.compare(first, second)>0) first else second
}
}
Scala提供了Context Bounds方法,写法如下:
class Pairs_Context_Bounds(first: T,second:T){
def bigger(implicit ordered: Ordering) ={
if (ordered.compare(first, second)>0) first else second
}
}
页:
[1]