Created
October 29, 2015 17:37
-
-
Save blast-hardcheese/5715b153c238044271ec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import shapeless._ | |
case class Location(lat: Double, lon: Double) | |
case class User(id: Long, username: String, location: Location) | |
case class Car(id: Long, location: Location) | |
object ImplicitLenses extends App { | |
implicit val userLocationLens: Lens[User, Location] = { | |
val ret = lens[User].location | |
ret | |
} | |
implicit val carLocationLens: Lens[Car, Location] = { | |
val ret = lens[Car].location | |
ret | |
} | |
// implicit val carLocationLens: Lens[Car, Location] = lens[Car].location | |
// produces: | |
// [info] Compiling 1 Scala source to /home/blast/.../target/scala-2.11/classes... | |
// [error] /home/blast/.../ImplicitTest.scala:66: polymorphic expression cannot be instantiated to expected type; | |
// [error] found : [B]mkLens.Out | |
// [error] required: shapeless.Lens[Car,Location] | |
// [error] implicit val carLocationLens: Lens[Car, Location] = lens[Car].location | |
// [error] ^ | |
// [error] one error found | |
// [error] (compile:compile) Compilation failed | |
// [error] application - | |
def resetLocation[T <: { def location: Location }](o: T)(implicit locationLens: Lens[T, Location]): T = { | |
locationLens.set(o)(Location(0, 0)) | |
} | |
resetLocation(User(5, "foo", Location(123, 456))) | |
resetLocation(Car(5, Location(234, 567))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment