aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/sams.scala
blob: 20338b95c9bd24e083d1d349ec2b8585fbbd028c (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
object test {

  trait X { def foo(x: Int): Int; def bar = foo(2) }

  val x: X = (x: Int) => 2  // should be a closure

  trait T {
    var f = 2
    def foo(x: Int): Int
  }

  val t: T = (x: Int) => 2   // needs to be an anonymous class because of defined field

  trait U extends T

  val u: U = (x: Int) => 2   // needs to be an anonymous class because of inherited field

  trait V extends Exception { def foo(x: Int): Int }

  val v: V = (x: Int) => 2   // needs to be an anonymous class because the trait extends a non-object class

  trait Y extends X {
    def baz = super.bar
  }

  val y: Y = (x: Int) => 2   // needs to be an anonymous class because of super accessor

  trait Z {
    def foo(x: Int): Int; println("hi there!")
  }
  trait ZZ extends Z

  val z: Z = (x: Int) => 2   // needs to be an anonymous class because trait has initialization code
  val zz: ZZ = (x: Int) => 2   // needs to be an anonymous class becaiuse trait has initialization code

  abstract class C {
    def foo(x: Int): Int

    trait I { def foo(x: Int): Int }

  }

  val c: C = (x: Int) => 2   // needs to be an anonymous class because C is not a trait

  val ci: c.I = (x: Int) => 2 // needs to be an anonymous class because it needs an outer pointer


  val pf: PartialFunction[Int, Int] = {
    case 1 => 1
    case 2 => 2
  }

  val qf: PartialFunction[(Int, String), Int] = {
    case (1, "abc") => 1
    case _ => 2
  }

  val rf: PartialFunction[(Int, AnyRef), Int] = {
    case (_: Int, _: String) => 1
    case _ => 2
  }

  val sf: PartialFunction[Any, Int] = {
    case x: String if x == "abc" => 1
  }
}

// From: neg/sammy_poly
// synthesizeSAMFunction where the sam type is not fully defined
class T {
  trait F[T, U] { def apply(x: T): U }
  // this is an inner trait, that will recieve an abstract $outer pointer. Not a SAM.
  def app[T, U](x: T)(f: F[T, U]): U = f(x)
  app(1)(x => List(x))
}

object SI9943 {

class Foo[T] {
  def toMap[K, V](implicit ev: Foo[T] <:< Foo[(K, V)]): Foo[Map[K, V]] = null
  def toMap[K](keySelector: T => K): Foo[Map[K, T]] = null
}

object Foo {
  val f: Foo[Int] = null
  val m = f.toMap(_ % 2)
}
}