- Learn Scala Programming
- Slava Schmidt
- 380字
- 2021-06-10 19:35:45
Type parameters
Type parameters are defined using square brackets []. If applied to classes and methods, they must be declared before normal parameters, and the result is known as a parameterized type:
case class Wrapper[A](content: A) {
def unwrap: A = content
}
def createWrapper[A](a: A):Wrapper[A] = Wrapper(a)
type ConcreteWrapper[A] = Wrapper[A]
val wInt: Wrapper[Int] = createWrapper[Int](10)
val wLong: ConcreteWrapper[Long] = createWrapper(10L)
val int: Int = wInt.unwrap
val long: Long = wLong.unwrap
The Wrapper class is parameterized by the A type. This type parameter is used to refer to the type of content in the unwrap method. The scope resolution rules apply to the type parameters the same way as they do to the normal parameters, as shown by the unwrap method definition.
The createWrapper method definition shows how the type parameter propagates to the implementation side—Wrapper(a) is parameterized with the A type by the compiler.
The ConcreteWrapper type definition shows that type aliases are parameterized in the same way that types are.
We then use our parameterized type to demonstrate that it is possible to provide explicit type parameters on the call side, as well as rely on type inference.
This type inference is in fact quite powerful. The compiler always tries to find the most specific type, as the following example reveals (I've provided the explicit type ascriptions, which reflect the types inferred by the compiler):
case class Abc[A](a: A, b: A, c: A)
val intA: Abc[Int] = Abc(10, 20, 30)
val longA: Abc[Long] = Abc(10L, 20L, 30L)
val whatA: Abc[AnyVal] = Abc(10, 20, true)
val whatB: Abc[io.Serializable] = Abc("10", "20", Wrapper(10))
val whatC: Abc[Any] = Abc(10, "20", Wrapper(10))
We discussed Scala's type hierarchy earlier, so it should be obvious how the compiler came up with the types shown in the preceding code snippet.
It is possible to restrict possible definitions of the type parameter by using type constraints, as shown in the following example:
trait Constraints[A <: AnyVal, B >: Null <: AnyRef] {
def a: A
def b: B
}
// compile error - type parameter bounds
// case class AB(a: String, b: Int) extends Constraints[String, Int]
case class AB(a: Int, b: String) extends Constraints[Int, String]
The compiler will check that the concrete definition conforms to the type parameter bounds.
- 密碼學原理與Java實現(xiàn)
- 單片機C語言程序設(shè)計實訓100例:基于STC8051+Proteus仿真與實戰(zhàn)
- 算法大爆炸:面試通關(guān)步步為營
- Mastering PHP Design Patterns
- Internet of Things with the Arduino Yún
- Learning Selenium Testing Tools(Third Edition)
- Learning Hunk
- 深入淺出Serverless:技術(shù)原理與應(yīng)用實踐
- Building RESTful Python Web Services
- Learning Apache Karaf
- 深入淺出Go語言編程
- ArcGIS for Desktop Cookbook
- Illustrator CS6設(shè)計與應(yīng)用任務(wù)教程
- 超簡單:用Python讓Excel飛起來(實戰(zhàn)150例)
- DB2SQL性能調(diào)優(yōu)秘笈