kotlin.Char
is defined in the following way:
public class Char private constructor() : Comparable<Char> {
...
}
Where Comparable<T>
is defined as:
public interface Comparable<in T> {
public operator fun compareTo(other: T): Int
}
So how comes a Char is Serializable
?
fun main(args: Array<String>) {
println('A'::class) // class kotlin.Char
println('A'::class.java) // char
println('A' is java.io.Serializable) // true
}
- Is it something added on compilation to Byte code?
- Is it documented anywhere?
The basic types such as Int
, Double
, Char
, etc. are represented on the JVM in one of two ways (documentation):
- whenever possible, as primitives (
int
, double
, char
),
- when required to be nullable or when used as generic type parameters, as the respective wrapper classes (
Integer
, Double
, Character
).
Both of these representations are serializable. Primitive types are serializable by default as they are, and their wrappers all implement Serializable
as well, for example, as you can see in the documentation of Character
.
This sort of mapping between Kotlin and Java types is also mentioned here in the Java interop documentation.
So the question is, why don't the Kotlin representations have Serializable
as a supertype on the source code level? My guess is so that they're kept platform independent, as having them explicitly implement java.io.Serializable
would make them depend directly on a JVM type.