Upper bound and lower bound
Declare Lower bound with:>, which is the parent class constraint, a:> B, a must be the parent of B
With <: declares Upper bound, i.e. subclass constraint, A <: b A must be a subclass of B
Examples of Lower bound
Package Ch6object Test4 {println ("Welcome to the Scala Worksheet") class A {type B;: List[int] def foo (A: B) = a} val x = new A {type b = Traversable[int]} Set (1) X.foo (set (1))//Val y = new A {type b = set[int ]}//Compilation Error}
Thus, the meaning of a:> B is: The parent class of B or a type with a common parent class with B
<:b is usually not used because it has the same effect as: B
2. High-order type
Like higher-order functions, sometimes a type requires another type to do the argument.
Type callback[t] = function1[t, Unit]
3.type Lambda
Similar to a function represented by a lambda expression, the type can be defined in such a flexible way, given an example from
Stackoverflow:http://stackoverflow.com/questions/8736164/what-are-type-lambdas-in-scala-and-what-are-their-benefits
Trait Monad[m[_]] {def point[a] (A:A): M[a] def Bind[a, B] (M:m[a]) (f:a = M[b]): M[b]} class Eithermonad[a] Extends monad[({typeλ[α] = either[a,α]}) #λ] {def point[b] (b:b): Either[a, B] def Bind[b, C] (M:either[a, B]) ( F:b = Either[a, c]): Either[a, C]}
4. For a high-order type a[t], the compatibility of a[t] varies with the specific type of T in three cases
Suppose s<: T
(1) Invariant <=> A[s] incompatible a[t]
(2) covariant <=> a[s] <: a[t]
(3) contravariant <=> a[t] <: A[s]
Original:
AHigher-kinded type that's invariant implies that for any typesT,A, andBIfT[a]c Onforms to T[B " Span style= "font-family: ' Newbaskerville-roman '; Font-size:10pt;color:rgb (0,0,0); Font-style:normal;font-variant: normal; " >then must be the equivalent type of b
T[a] Conforms T[b] can be understood as a reference to a type of T[a] object that can be assigned to the T[B] type
covarianceRefers to the ability to substitute a type parameter with its parent type:
For any typesT,AandBi F conforms to then
The type of 5.mutable must be invariant. such as array
Type of parameter of type 6.Function, must be contraviriant
Package ch6object test4 { println ("welcome to the scala Worksheet ") //> Welcome to the Scala Worksheet def foo (X: any):string = "hello, i received a " + x //> foo: (X: any) String def bar (x: string): any = foo (x) //> bar: (x: string) Any bar ("test") //> res0: any = hello, i received a test foo ("Test ") //> res1: string = hello, i received a test}
Foo is of type Function1[any, String]
Bar is of type function1[string, any]
Foo can be assigned a value to bar
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/94/wKioL1Se2g2AIsb-AACHBXxeg_k484.jpg "title=" 2014-12-28 00_07_21-Untitled-Paint. png "alt=" wkiol1se2g2aisb-aachbxxeg_k484.jpg "/>
Scala in depth 6 Scala's type system