Scala类型限定

 1 package big.data.analyse.scala
 2 
 3 /**
 4   * 类型限定
 5   * Created by zhen on 2018/12/9.
 6   */
 7 object Lxxd {
 8   def main(args: Array[String]) {
 9     // A =:= B 表示A,B类型相同
10     def it_is_equal[T](i:T)(implicit ev : T =:= Int){
11       println("life is short, you need python...")
12     }
13     it_is_equal(21)
14     // it_is_equal("python") //Error:(14, 16) Cannot prove that String =:= Int.
15     // A <:< B 表示A类型是B类型的子类型
16     def it_is_more[T](i:T)(implicit ev : T <:< java.io.Serializable){
17       println("life is short, you need spark...")
18     }
19     // it_is_more(12) // Error:(19, 15) Cannot prove that Int <:< java.io.Serializable.
20     it_is_more("spark")
21   }
22 }

结果:

原文地址:https://www.cnblogs.com/yszd/p/10093325.html