summaryrefslogtreecommitdiff
path: root/test/files/pos/overloaded_ho_fun.scala
blob: 17176715f0f1153a17c3d04aeac53287ca4f4cb5 (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
import scala.math.Ordering
import scala.reflect.ClassTag

trait Sam { def apply(x: Int): String }
trait SamP[U] { def apply(x: Int): U }

class OverloadedFun[T](x: T) {
  def foo(f: T => String): String = f(x)
  def foo(f: Any => T): T = f("a")

  def poly[U](f: Int => String): String = f(1)
  def poly[U](f: Int => U): U = f(1)

  def polySam[U](f: Sam): String = f(1)
  def polySam[U](f: SamP[U]): U = f(1)

  // check that we properly instantiate java.util.function.Function's type param to String
  def polyJavaSam(f: String => String) = 1
  def polyJavaSam(f: java.util.function.Function[String, String]) = 2
}

class StringLike(xs: String) {
  def map[A](f: Char => A): Array[A] = ???
  def map(f: Char => Char): String = ???
}

object Test {
  val of = new OverloadedFun[Int](1)

  of.foo(_.toString)

  of.poly(x => x / 2 )
  of.polySam(x => x / 2 )
  of.polyJavaSam(x => x)

  val sl = new StringLike("a")
  sl.map(_ == 'a')  // : Array[Boolean]
  sl.map(x => 'a')  // : String
}

object sorting {
  def stableSort[K: ClassTag](a: Seq[K], f: (K, K) => Boolean): Array[K] = ???
  def stableSort[L: ClassTag](a: Array[L], f: (L, L) => Boolean): Unit = ???

  stableSort(??? : Seq[Boolean], (x: Boolean, y: Boolean) => x && !y)
}

// trait Bijection[A, B] extends (A => B) {
//   def andThen[C](g: Bijection[B, C]): Bijection[A, C] = ???
//   def compose[T](g: Bijection[T, A]) = g andThen this
// }

object SI10194 {
  trait X[A] {
    def map[B](f: A => B): Unit
  }

  trait Y[A] extends X[A] {
    def map[B](f: A => B)(implicit ordering: Ordering[B]): Unit
  }

  trait Z[A] extends Y[A]

  (null: Y[Int]).map(x => x.toString) // compiled
  (null: Z[Int]).map(x => x.toString) // didn't compile
}