Welcome
Welcome to the KotlinPoetDSL-guide. This guide will focus initially on the difference between KotlinPoetDSL and KotlinPoet.
The goal of KotlinPoetDSL
The main goal of KotlinPoetDSL is to generate code, with code like the generated code.
The main way to do this is by providing dumb wrappers around KotlinPoet. There are however some smart features that KotlinPoet doesn't have (yet), like constructorSpec, which adds val- or var-propperties straight away.
The best way to demonstrate this is using an example.
Code to generate.
Let's say we want to generate the follolwing code:
Using KotlinPoet
In KotlinPoet we use the following code:
Using KotlinPoetDSL
The same code:
Generated using KotlinPoetDSL will look like:
Evaluation
As you can see, KotlinPoet is very composable and is ideal for slowly building up everything. When you write everything at once, it isn't very readible.
On the other hand, KotlinPoetDSL is very readible when you write everything at once. With DSL's it's on the other hand, not that friendly to compose together.
This means that KotlinPoetDSL does not replace KotlinPoet, but makes it stronger on a different point. Because KotlinPoetDSL makes use of KotlinPoet, and uses KotlinPoets type throughout the wrapper, you can easily use them together.
Last updated