aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/implicits1.scala
blob: eda1346630d2dec6591f0c10a198a72c55fa8e56 (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
class X(val elem: Int) {
  def foo(y: String): Int = y.length + elem
}

object X {
  implicit class BarDeco(x: X) {
    def bar: String = "!"
  }
}

object Implicits {

  implicit val impl: X = new X(0)

  implicit def conv(x: Int): X = new X(x)

  class Xdecorator(x: X) extends Object {
    def foo(cond: Boolean): Int = if (cond) x.foo("abc") else 0
  }

  implicit def XDecorator(x: X): Xdecorator = new Xdecorator(x)

  val a: Object = "abc"
  val b: Any = "abc"

  def foo(x: Int)(implicit y: X): Int = {
    println(y)
    x
  }

  val y: Int = foo(1)

  val z: X = 3

  val c: Int = y.elem

  val d: Int = z.foo("abc")

  val x: X = Byte.MinValue

  //import X.BarDeco

  println(z.bar)

  val e: Int = z.foo(true)

  // Haoyi Li's example on scala-user:

  trait Modifier

  implicit def stringNode(v: String): Modifier = new Modifier {}

  val s: Modifier = Some("rd").getOrElse("")

  val xx: Int = (1: Byte)

}