Created
May 30, 2017 04:10
-
-
Save tejancsu/f89cc5b70093a9dd518faedac67e450b to your computer and use it in GitHub Desktop.
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
case class MyShow[T](f: T => String) extends AnyVal | |
trait LowPriorityShow { | |
implicit def showAnything[T] = MyShow[T](_.toString) | |
} | |
object MyShow extends LowPriorityShow { | |
implicit val showDateTime = MyShow[DateTime](ISODateTimeFormat.dateTime().withZone(DateTimeZone.forID("America/New_York")).print _) | |
implicit def showOpt[T] = MyShow[Option[T]](_.fold("")(_.toString)) | |
def show[T](v: T)(implicit s: MyShow[T]) = { | |
s.f(v) | |
} | |
} | |
println(MyShow.show(DateTime.now())) | |
//2017-05-30T00:08:22.851-04:00 -- This seems to pick the showDateTime implicit | |
println(MyShow.show(Some("abc"))) -- This picks showAnything instead of showOpt implicit | |
//Some(abc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Probably late to the party, but your issue here, is that the implicit resolution picks up the type Some[String] for the second case.
try this and you should be good for this case,
println(MyShow.show(Some("abc"): Option[String]))
in libraries like cats, you can see that there are some implicits to allow you to do "abc".some <- which defines the type result as Option[String] not leaving to the compiler to do type inference as it will result in Some[String]
alternatively, playing with variance should do the trick