Kotlinx-metadata - properties out of order?

I have this simple class:

data class Sample(val id: String, val num: Int, val date: Date)

But, when I’m looking from an annotation processor, the KmClass.properties has them in this order: date, id, num.

perhaps because of sorting, but how do I get the original order?

1 Like

My guess is that they are sorted alphabetically. As far as I know kotlin doesn’t save the original property order directly. The only way I can think of is using the constructor parameters to get the order of propertis, but maybe I am missing something.

2 Likes

This is a known issue (KT-41042). All declarations in the class are currently sorted by name. Indeed, the only way to get the original order is to inspect the order of constructor parameters.

By the way, in certain cases it would even be more correct to do so, rather than to rely on the order of properties in the class, even if the properties were in source order. For example, if some application loads metadata via kotlinx-metadata-jvm and performs something non-trivial based on the property order, it would be strange if changing the order of properties outside of the primary constructor would somehow change behavior in that application.

2 Likes