Scala から Java API を呼び出す方法を見つけようとしています。基本的に、 、 のContentValues
ようないくつかのメソッドを持つオブジェクトがありgetAsString
、getAsLong
それぞれが独自の異なる戻り値の型を持っています。
T に応じて正しいメソッドを呼び出すメソッドをContentValues
追加できるように、別のオブジェクトをラップできますか?get[T]
getAsXXX
私が試したこと(うまくいかなかった、あいまいな暗黙の解決について不平を言う):
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 _))
}