Ran Magen
01/03/2020, 7:29 PMT
types that are used when invoking this:
fun <T> createFetcher(name: String, underlying: DataFetchingEnvironment.() -> T): NamedFetcher[T] { .. }
Now I decide I want to be able to wrap it with some caching. For that I need to ensure T has a codec to/from bytes. In Scala I would use a type class, e.g.:
trait Codec[T] {
def encode(t: T): ByteArray
def decode(bytes: ByteArray): T
}
and then implement the wrapper such that T
must have a corresponding concrete Codec[T]
, e.g.
class Cache[T: Codec] {
def putIfAbsent(key: String, value: T): T = { val encoded = implicitly[Codec[T]].encode(value); bytesCache.putIfAbsent(...); value }
}
def createFetcher[T: Codec](name: String, underlying: DataFetchingEnvironment.() -> T, key: DataFetchingEnvironment.() -> String): NamedFetcher[T] = {
val env = makeEnv(name)
cache.putIfAbsent(key(env)) { underlying(env) }
}
StavFX
01/04/2020, 1:20 AMHanno
01/04/2020, 12:12 PMHanno
01/04/2020, 12:13 PMHanno
01/04/2020, 12:15 PMHanno
01/04/2020, 12:16 PM