Scala List的协变特性——泛型上界与下界

deltamaster posted @ Jun 19, 2014 09:36:57 PM in Scala with tags scala 函数 泛型 Functional Programming Liskov 协变 逆变 , 7562 阅读


trait List[+T] {
  def isEmpty: Boolean
  def head: T
  def tail: List[T]

class Cons[T](val head: T, val tail: List[T]) extends List[T] {
  def isEmpty = false

object Nil extends List[Nothing] {
  def isEmpty: Boolean = true
  def head: Nothing = throw new NoSuchElementException("Nil.head")
  def tail: Nothing = throw new NoSuchElementException("Nil.tail")


  val x: List[String] = Nil
  val ages: List[Int] = new Cons(16, new Cons(22, Nil))



trait List[+T] {
  // omit other methods
  def prepend(elem: T): List[T] = new Cons(elem, this)




trait List[+T] {
  // omit other methods
  def prepend[U >: T](elem: U): List[U] = new Cons(elem, this)




  • 协变类型可以作为泛型类型的下界
  • 逆变类型可以作为泛型类型的上界
* 本文在CC BY-SA(署名-相同方式共享)协议下发布。
AP SSC Evs Question 说:
Sep 11, 2022 03:55:09 PM

Advised to everyone can contact the class teacher to get important questions for all lessons and topics of EVS. Every Telugu Medium, English Medium and Urdu Medium student of the State Board can download the AP 10th Class EVS Model Paper 2023 Pdf with answers for term-1 & term-2 exams of SA-1, SA-2 and other exams of the board. AP SSC Evs Question Paper Environmental Education is one of the most important subjects and it’s a part of Science. School Education Department and various leading private school teaching staff have designed and suggested the practice question paper for all Part-A, Part-B, Part-C, and Part-D questions of SA-1, SA-2, FA-1, FA-2, FA-3, FA-4 and Assignments.

reese 说:
Nov 28, 2022 08:10:14 PM

In Scala, the List class has generic upper and lower bounds. This means that the List class can be used with any type that is a subtype of the si joint pain upper bound or a supertype of the lower bound. For example, if the upper bound is set to Any, then the List class can be used with any type. If the lower bound is set to Nothing, then the List class can be used with any type that is a supertype of Nothing.

charlly 说:
Jan 18, 2023 12:17:42 PM

There are many reasons why you might want to order a custom essay. Maybe you're struggling with the material and need a little extra help. Maybe you're a great student but just don't have the time to write an essay. Maybe you need a specific type of essay that isn't available anywhere else. Whatever the reason, what are the different cuts of diamonds there are many places you can go to order a custom essay.

WiFi Names 说:
Feb 03, 2023 06:35:53 PM

Are you getting yourself a new WiFi router, then you might be happy because now you are able to set up your best WiFi names to your liking which is a funny act but a please to do indeed. WiFi Names Well as you already know that the reason why people be objective about finding the best WiFi names for their new connections or routers is that they want some cool or funny names that make them feel nice and at the same time when your friends, family.

登录 *

loading captcha image...
or Ctrl+Enter