-
Notifications
You must be signed in to change notification settings - Fork 0
/
traitplay.scala
42 lines (34 loc) · 903 Bytes
/
traitplay.scala
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
object foo {
trait Gettable[T] {
def get: T
}
case class Box[T](item: T) extends Gettable[T] {
def get = item
}
object bar {
def makeBox[T](item: T) = Box(item)
}
type MakeBox[T] = T => Box[T]
val makeBoxedInt: Int => Box[Int] = Box(_)
val makeBoxedInt2: MakeBox[Int] = Box(_)
// this one does not compile
// val makeBoxed[T]: T => Box[T] = Box(_)
def makeBoxed[T]: T => Box[T] = Box(_)
// if you put makeBoxed in the REPL you get:
// Nothing => foo.Box[Nothing] = <function1>
def makeBoxed2[T]: MakeBox[T] = Box(_)
object ByClosure {
object Boxer {
def make[T]: T => Box[T] = makeBoxed
}
}
object ByInjection {
// fails, noting that existential
// type import is required
// case class Boxer(makeBox: MakeBox[_])
case class Boxer[T](makeBox: MakeBox[T]) {
def make(item: T): Box[T] = makeBox(item)
}
val intBoxer: Boxer[Int] = Boxer(makeBoxed)
}
}