0 votes
in Kotlin by
How would you refactor this code using apply?

Problem

Consider:

class Message(message: String, signature: String) {

  val body = MessageBody()

  

  init {

    body.text = message + "\n" + signature

  }

}

Do you see any refactoring that could be done?

1 Answer

0 votes
by
You can write:

class Message(message: String, signature: String) {

  val body = MessageBody().apply {

    text = message + "\n" + signature

  }

}

Related questions

0 votes
0 votes
0 votes
asked Aug 24, 2023 in Apache Superset by Robin
0 votes
asked Sep 3, 2022 in Ruby by DavidAnderson
...