blob: 9cee1889316eee482321aec31f1e11eb456e2fa4 (
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
|
object Test extends dotty.runtime.LegacyApp {
trait MyPF[@specialized(Int) -A] extends (A => Unit) {
def isDefinedAt(x: A): Boolean
def applyOrElse[A1 <: A](x: A1, default: A1 => Unit): Unit = {
println("MyPF.applyOrElse entered...")
if (isDefinedAt(x)) apply(x) else default(x)
}
}
trait MySmartPF[@specialized(Int) -A] extends MyPF[A] {
def apply(x: A): Unit = {
println("MySmartPF.apply entered...")
applyOrElse(x, { default: Any => throw new MatchError(default) })
}
}
type T = Int
//type T = Any
def newPF(test: T): MyPF[T] = new MySmartPF[T] {
def isDefinedAt(x: T): Boolean = x != test
override def applyOrElse[A1 <: T](x: A1, default: A1 => Unit): Unit = {
println("newPF.applyOrElse entered...")
if (x != test) { println("ok"); () } else { println("default"); default(x) }
}
}
val pf = newPF(1)
println("=== pf(1):")
try { pf(1) } catch { case x: Throwable => println(x) }
println("=== pf(42):")
pf(42)
println("=== done")
}
|