holgerbrandl
11/12/2021, 8:31 AMfun interface SmthgSAMGeneric<T> {
fun foo(): (T, T) -> String
}
val samAsObject = object : SmthgSAMGeneric<String> {
override fun foo(): (String, String) -> String {
return { s: String, s2: String -> "42" }
}
}
val inlineSam = SmthgSAMGeneric<String> {
{ s: String, s2: String -> "42" }
}
See also https://github.com/EsotericSoftware/kryo/issues/867bashor
11/17/2021, 8:06 PMdmitry.petrov
12/10/2021, 9:18 AMval samAsObject = object : SmthgSAMGeneric<String> {
override fun foo(): (String, String) -> String {
return { s: String, s2: String -> "42" }
}
}
is just an anonymous class implementing SAM interface.
val inlineSam = SmthgSAMGeneric<String> {
{ s: String, s2: String -> "42" }
}
is a SAM conversion on lambda, which is generated as invokedynamic with java.lang.invoke.LambdaMetafactory. Corresponding class has non-stable class name, is not accessible by name via reflection, and is not serializable unless corresponding SAM interface extends Serializable (there's special magic in LambdaMetafactory for serialization support). But, well, if you work with kryo, you probably know that all already.holgerbrandl
12/10/2021, 4:44 PMdmitry.petrov
12/11/2021, 5:42 AMfun interface SmthgSAMGeneric<T> : Serializable {
fun foo(): (T, T) -> String
}
Then SAM conversions to SmthSAMGeneric
will become serializable. Kryo knows about related LambdaMetafactory "magic" and can handle such serialized SAM wrappers.holgerbrandl
12/14/2021, 9:51 PM