aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/inferred.scala
blob: 311e56a50420b68f12ba08c052eafe145df303a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
abstract class LIST[+T] {

  def isEmpty: Boolean
  def head: T
  def tail: LIST[T]

  def prepend [U >: T] (x: U): LIST[U] = new CONS(x, this)

  def map[U](f: T => U): LIST[U] = if (isEmpty) NIL else tail.map(f).prepend(f(head))

}

object NIL extends LIST[Nothing] {
  def isEmpty = true
  def head = throw new Error
  def tail = ???
}

class CONS[U](hd: U, tl: LIST[U]) extends LIST[U] {
  def isEmpty = false
  def head: U = hd
  def tail = tl
}

object Inferred {

  def foo[T](x: T): T = x

  val x = foo(1)

  val y = foo("abc")

  def bar[U](xs: LIST[U]): LIST[U] = xs

  val n = NIL

  val nn = bar(NIL)

  val ints: LIST[Int] = NIL prepend 1

  val ints1 = NIL prepend 1 prepend 2

  val a = if (1 == 0) NIL else ints

  val n2 = scala.collection.immutable.Nil

  val ss2: scala.collection.immutable.List[String] = "abc" :: n2

  val ss3 = "abc" :: n2

  def cl = ((x: Int) => x + 1)

  val ints2 = ints map (_ + 1)

  val ints3 = new CONS[Int](1, NIL)

  val ints4 = new CONS(1, NIL)
}