Last active
June 28, 2017 07:46
-
-
Save kastoestoramadus/4d1dc6096aaa84386334998a233e2054 to your computer and use it in GitHub Desktop.
Scala implicitly. Where to override it?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package snippet | |
/** | |
* Created by walidus on 27/06/17. How to override the implicit val with c? | |
*/ | |
object ImplicitTest extends App{ | |
implicit val overrider = MyClass.c // works | |
Util.print() | |
} | |
object Util { | |
def print() = { | |
implicitly[Gen[MyClass]].printSomething // autogenerated code, assume you can not change it | |
} | |
} | |
class Gen[T](str: String) { | |
// bad place //implicit val overrider = MyClass.c | |
def printSomething = println(str) | |
} | |
case class MyClass() | |
object MyClass { | |
implicit val b = new Gen[MyClass]("MyClass") // this we want to override | |
val c = new Gen[MyClass]("overriden MyClass") // this is what we want use instead for minority of useage | |
} | |
// MyClass and package object have to be in separate files | |
package object snippet { | |
// too big scope //implicit val overrider = MyClass.c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment