alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (implicits.scala)

This example Scala source code file (implicits.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, app, c, c, d, d, impl, inner, int, int, ok, string, string, test

The Scala implicits.scala source code

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

Other Scala examples (source code examples)

Here is a short list of links related to this Scala implicits.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.