0

Now i can generate code like this

val builder = MyBuilder()
builder.setA()
builder.setB()
builder.setC()
val target = builder.build()

I want to generate code like under

val target = MyBuilder()
  .setA()
  .setB()
  .setC()
  .build()
Jade
  • 416
  • 5
  • 7
  • 1
    I wouldn't worry about it. Generated code doesn't have to be beautiful. – Jorn Jul 18 '23 at 07:40
  • But if you really want an answer, it might be useful to show the KotlinPoet code you have, what you tried to achieve this yourself, and why it didn't work. – Jorn Jul 18 '23 at 07:41
  • you would be able to do something like that, but your `setX` functions would have to return the class itself, which is yucky. Another things you could do is `MyBuilder().apply { setA(); setB(); setX() }`. You'd probably want to replace the `;` with new lines, can't format comments sadly. – AlexT Jul 18 '23 at 08:04
  • @Jorn thanks for your suggest, I just want to generate code trimly。now i use my first plan, call builder.setX line by line. it work fine. – Jade Jul 18 '23 at 11:12

0 Answers0