Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cant understand this kotlin companion object code

I went through this code example but i can’t get it to run neither do understand what it does exactly.

data class Order(
    val id: String,
    val name: String,
    val data:String
)

data class OrderResponse(
    val id: String,
    val name: String,
    val data: String
) {
    companion object {
        fun Order.toOrderResponse() = OrderResponse(
            id = id,
            name = name,
            data = data ?: "",
        )
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function in the companion object extends Order with a help function to turn Order instances into OrderResponse instances. So for example like

val order = Order("a", "b", "c")
val orderResponse = order.toOrderResponse()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading