I'm trying to figure out a way to call a Java API from Scala. Basically, there's a ContentValues
object that has a couple of methods like getAsString
, getAsLong
, each having their own different return type.
Can I wrap ContentValues
in another object so that I can add a get[T]
method that calls the correct getAsXXX
method depending on T?
What I tried (didn't work, complains about ambiguous implicit resolution) :
object SContentValuesConversions {
case class SContentGetter[ScalaType](val check: String => Boolean, val getter: String => ScalaType) {
def getTyped(key: String): Option[ScalaType] = {
if (!check(key)) None
else Some(getter(key))
}
}
implicit def SIntContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsInteger _))
implicit def SLongContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsLong _))
implicit def SStringContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsString _))
}