类型推断和泛型

Scala 编译器还可以在调用多态方法时或在实例化泛型类时推导出类型参数:

case class InferedPair[A, B](a: A, b: B)

val pairFirstInst = InferedPair("Husband", "Wife")  //type is InferedPair[String, String]
    
// Equivalent, with type explicitly defined
val pairSecondInst: InferedPair[String, String] 
                      = InferedPair[String, String]("Husband", "Wife")  

上述类型推断形式类似于 Java 7 中引入的 Diamond Operator