Scala for Java Refugees Part 5: Traits and Types class MyMap[V<:Component] extends HashMap[String,V]...

Please Visit: http://ift.tt/1ajReyV

Scala for Java Refugees Part 5: Traits and Types
class MyMap[V<:Component] extends HashMap[String,V]
the single type parameter must be a subtype of Component
class MyMap[V:>CollegeStudent] extends HashMap[String,V]
Lower type bounding is when the type parameter is constrained such that a certain type must inherit from the type parameter.
type parameter V can be any type which is extended by CollegeStudent .
Thus the parameter could be Student, Worker or even Person.
val str:String = obj.asInstanceOf[String]
http://ift.tt/1bTlOqX

An upper type bound T <: A declares that type variable T refers to a subtype of type A.
lower type bounds declare a type to be a supertype of another type. The term T >: A expresses that the type parameter T or the abstract type T refer to a supertype of type A.
http://ift.tt/1zGuGM3
case class ListNode[+T](h: T, t: ListNode[T]) {
  def head: T = h
  def tail: ListNode[T] = t
  def prepend[U >: T](elem: U): ListNode[U] =
    ListNode(elem, this)
}


from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1bTlN6l
via LifeLong Community

No comments:

Post a Comment