:wave: Is it possible to write a compiler plugin t...
# compiler
e
👋 Is it possible to write a compiler plugin that adds a Builder pattern a la lombok's @Builder? I understand that it's not needed if you stay in the Kotlin world, but for migrating a large codebase that uses lombok to Kotlin, it would help a lot if you could use builders in Java to create instances of Kotlin classes.
👌 1
Thanks for the MVA (minimal viable answer) 😄 I'll try to dig my way through the scarce docs and articles.
BTW: A testing framework in Kotlin similar to Spock would be awesome, but that would require expert understanding of compiler plugins and a whole lot of effort, I guess.
r
here is an example that shows how to deal with IR to inject calls and perform implicit instantiations for a proof system https://github.com/arrow-kt/arrow-meta/blob/master/compiler-plugin/src/main/kotlin/arrow/meta/plugins/proofs/phases/ir/ProofsIrCodegen.kt In your case you’d have to work with similar APIs if your plugin targets the IR backend.
The notion is that you’d define what @Builder does and in the IR phase transform the tree to a new tree which includes whatever boilerplate you want to inject
e
Thanks a lot! Let's see how far I get.
r
The key piece is parts like this https://github.com/arrow-kt/arrow-meta/blob/master/compiler-plugin/src/main/kotlin/arrow/meta/plugins/proofs/phases/ir/ProofsIrCodegen.kt#L14[…]L152 where you can see that an initializer or dispatch receiver of an expression can be mutated and changed allowing you to alter the tree before it turns into bytecode
s
I believe you can implement Builder even without a compiler plugin. It is also achievable with annotation processing. But it is certainly possible with a compiler plugin
e
@Sasha Shpota Yes, you can - kinda. See https://github.com/ThinkingLogic/kotlin-builder-annotation For full support of these, I suspect you need a compiler plugin: • default values • nullability • mutability of collections • lombok-like SomeClass.builder() method Besides, I just want to try it for fun 🙂