Is class value properties interoperable with Java?


#1

I assumed that:

    data class Order(val state: String) {
        fun isComplete() = (state == "Fulfilled")
    }

is the same as:

    data class Order(val state: String) {
        val isComplete = (state == "Fulfilled")
    }

or at least:

    data class Order(val state: String) {
        val isComplete: Boolean

        init {
            isComplete = (state == "Fulfilled")   
        }
    }

but they aren’t if it comes to interoperability with Java. Then only the first version works. Rest of them returns null
I thought that I’m doing something wrong but I used also the “java to kotlin” tool to check if this is me misunderstanding something. I checked that against Kotlin version 1.1.2 and 1.1.3 within the android project


#2

Could you provide an example of code where null is returned when you think it shouldn’t be?


#3
@Override
public void onRequestSuccess(Order responseOrder) {
    if (responseOrder.isComplete()) {
        sendSuccessfulPaymentLog();
    }
    ....
}

Hah maybe this is caused by the creation of Order object by the GSON library. That’s how these object get instatiated.

return Gson().fromJson(order.mergeLinks(links), Order::class.java)

#4

Gson doesn’t run property initializers. A way to convert your original code to use a property while preserving semantics is the following:

data class Order(val state: String) {
    val isComplete get() = (state == "Fulfilled")
}

#5

Thank you @yole