File: implicits.scala

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (50 lines) | stat: -rw-r--r-- 822 bytes parent folder | download | duplicates (4)
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
import scala.language.implicitConversions

object A {
  object B {
    implicit def int2string(x: Int) = "["+x.toString+"]"
  }
}

class C(x: String) {

  class Inner {
  }

  object Inner {
    val s: String = x
    implicit def Inner2String(x: Inner): String = s
  }
}

object Test extends App {
  import A.B._
  val c = new C("OK")
  val i = new c.Inner
  val s: String = i
  Console.println(s)
  Console.println(2: String)
}

object TestPriority {

  class C(x: Int) { def foo: Int = x + 1 }

  class D(x: Int) { def foo: Int = x + 2 }

  class IMPL {
    implicit def Int2C(x: Int): C = new C(x)
  }

  object impl extends IMPL {
    implicit def Int2D(x: Int): D = new D(x)
  }

  import impl._

  val x: C = 2
  val y: D = 2
  assert(x.foo == 3, x.foo)
  assert(y.foo == 4, y.foo)
  assert((2).foo == 4, (2).foo)
}