I’m porting my Vaadin app to Kotlin, converting Vaadin listeners into closures. All Vaadin listeners are Serializable; I’m therefore wondering whether the Kotlin closures are serializable or not. I have found no doc on this; I tried running this code and it seems working properly:
fun Any.serializeToBytes(): ByteArray = ByteArrayOutputStream().use { it -> ObjectOutputStream(it).writeObject(this); it }.toByteArray()
inline fun <reified T: Any> ByteArray.deserialize(): T = ObjectInputStream(inputStream()).readObject() as T
inline fun <reified T: Any> T.serializeDeserialize() = serializeToBytes().deserialize<T>()
{ println("foo") }.serializeDeserialize()()
prints “foo” correctly. However, when calling Kotlin code from Java, the Functionx interfaces are not themselves marked serializable.
Thank you