Welcome to the Scala discussion QQ group 212859367, and Lianle together to discuss learning!
Generic class
Example:
class Pair[T, S](val first: T, val second: S)
Generic functions
Example:
def getMiddle[T](a: Array[T] = a(a.length / 2))
Scala infers the type from the actual arguments that the method is called to use.
getMiddle(Array("Mary", "had", "a", "little", "lamb"))//将会调用getMiddle[String]
Type variable definition
Fix the problem: Sometimes you need to limit the type variable.
class Pair[T <: Comparable[T]](val first: T, val second: T) { def smaller = if (first.compareTo(second) < 0) first else second}
In the example above, T must be a subtype of comparable[t].
View definition
Example:
class Pair[T <% Comparable[T]]//意味着T可以被隐式转换成Comparable[T]
Context definition
The view definition t<%v requires that an implicit conversion from T to V be present.
The context is defined in the form of: T:m, where M is another generic class, and it requires an "implicit value" of type m[t] to exist.
Example:
class Pair[T : Ordering]
class Pair[T: Ordering](val first T, val second: T) { def smaller(implicit ord: Ordering[T]) = if (ord.compare(first, second) < 0) first else second}
Implicit values are more flexible than implicit conversions.
Manifest Context Definition
To instantiate a generic array[t], we need a manifest[t] object.
Example:
def makePair[T: Manifest](first: T, second: T) { val r = new Array[T](2) r(0) = first r(1) = second; r}
If you call Makepair (4, 9), the compiler navigates to the implicit manifest[int] and actually calls Makepair (4,9) (intmanifest), so that the calling method calls the new Array (2) ( Intmanifest), returns an array of primitive types: int[2]
Multiple definitions
Type variables can have both upper and lower bounds:
T >: Lower <: Upper
You cannot have multiple upper bounds or multiple lower bounds at the same time.
You can require a type to implement multiple traits:
withwith Cloneable
There can be multiple views defined, as follows:
T <% Comparable[T] <% String
There can also be multiple contexts defined, as follows:
Ordering : Manifest
Type constraints
T =:= UT <:< UT <%< U
The above constraint will test whether T equals U, whether it is a subclass of U, or whether it is converted to u by the view (implicit).
Example:
class Pair[T](val first: T, val second: T<:<Comparable[T])
Type constraints allow you to define methods in generics that can be used only under certain conditions.
class Pair[T](val first: T, val second: T){ def smaller(implicit ev: T <:< Ordered[T]) = if (first < second) first else second}
Type Change
Example:
class Pair[+T] (val first: T, val second: T)
Description: The plus sign means that the type is covariant with T, that is, it changes in the same direction as T.
There can also be a type change in another direction.
trait Friend[-T] { def befriend(someone: T)}
In a generic type declaration, you can use both types of changes at the same time.
Covariant and contravariant points
Typically, the value that is consumed for an object applies contravariance, whereas for the value it produces, the covariance is applied.
object cannot be generic
We cannot add a type parameter to an object. such as variable lists.
Type wildcard character
In Java:
voidextends Person> people)
In Scala:
def process(people: java.util.List[_ <: Person])
def makeFriends(p: Pair[_ <: Person])
The wildcard character of the inversion is used:
import java.util.Comparatordef min[T] (p: Pair[T])(comp: Comparator[_ >: T])
Welcome to the Scala discussion QQ group 212859367, and Lianle together to discuss learning!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Learn scala-with Lianle. Type parameters