aboutsummaryrefslogblamecommitdiff
path: root/tests/pos/implicits1.scala
blob: 47b7f1c522d05fcb137f6e3c2fa2c419d566e991 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                           
 





                                

                  


                                         
 



                                                               
                                                               
 









                                         

              


                           
 
                    

                
 








                                                                
 
 
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")

  //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("")

}