fork(1) download
  1. object Main extends App {
  2.  
  3. class A(val a: String) { override def toString = s"A: $a"}
  4.  
  5. object A {
  6. implicit val default: A = new A("A") //found by default
  7. }
  8.  
  9. object B {
  10. def func(fn: => Int)(implicit a: A) = println(a)
  11. }
  12.  
  13. class Broken {
  14. def doSomething = {
  15. import Broken._ // or Broken.actual
  16. B.func { 123 } // Uses A.default, not Broken.actual for implicit
  17. }
  18. }
  19.  
  20. object Broken {
  21. implicit val actual: A = new A("Broken")
  22. }
  23.  
  24. class Fixed {
  25. def doSomething = {
  26. import Fixed._
  27. println(actual) //reference actual
  28. B.func { 123 } // uses Fixed.actual
  29. }
  30. }
  31.  
  32. object Fixed {
  33. implicit val actual: A = new A("Fixed")
  34. }
  35.  
  36. object WTF {
  37. implicit val actual: A = new A("WTF")
  38. }
  39.  
  40. class WTF {
  41. def doSomething = {
  42. import WTF._
  43. B.func { 123 } // With object definition first this works without referencing actual
  44. }
  45. }
  46.  
  47. (new Broken).doSomething
  48. }
Success #stdin #stdout 0.38s 382016KB
stdin
Standard input is empty
stdout
A: Broken