Kotlin.String, kotlin.Int, etc. do not implement java.io.Serializable


#1
class Foo<T: java.io.Serializable>
fun f() {
    Foo<String>() // does not compile
    Foo<java.io.String>() // compiles
}

e.g., spring data uses this for specifying the ID of the repository

public interface MongoRepository <T, ID extends java.io.Serializable> …

is this by design? should I file a bug?

EDIT: fixed missing type args


#2

Something's wrong with your example:

class Foo
fun f() {
    Foo() // does not compile
    Foo() // compiles
}

Both options compile for me


#3

sorry, the forum software ate the type arguments :-)


#4

I see the problem now, yes, file an issue please


#5

thanks for the prompt reply, will do


#6

This is still the case in Kotlin 1.2!


#7

The code works fine in Kotlin 1.2

class Foo<T : java.io.Serializable>

fun f() {
    Foo<String>() // compiles
    Foo<java.lang.String>() // compiles, and warning suggesting to use kotlin.String
}

In fact, you can check the String implements java.io.Serializable by running this code:

import kotlin.reflect.full.allSuperclasses

fun main(args: Array<String>) {
    println(String::class.allSuperclasses)
}

Which prints:

[class kotlin.Comparable, class kotlin.Any, class kotlin.CharSequence, class java.io.Serializable]

#8

Indeed, both String an Int are internally transformed into appropriate Java classes which are serializeable.
I’ve recently encountered another problem. KClass is not serializable, so I had to use Java Class instead. I do not see any reason not to make KClass serializable as well.