Intro to CodeBlock

What is it?

The code block is a class that let's you generate code that acts instead of constructs. In other words, it's the place where you can place if. This is inside a function, but also in the initialization of properties. This is nothing different from KotlinPoet.

The goal of KotlinPoetDSL

The goal of KotlinPoetDSL with these code-blocks is to make the control-flows as much build in as possible. Build in and tested features means you won't write mistakes when you have to write them yourselves.

Generating

The codeBlock can both be generated implicitly and explicitly

Implicit generation

The codeBlock is implicitly build with alll the functions: getters, setters, constructors and the normal functions.

buildClass {
    clazz("Hi"){
        prop("prop".varOf<String>{
            // At the moment I do not, but from V0.2 i will
        }){
            getter { /* I create a CodeBlock */ }
            setter { /* Me too! */ }
        }

        func("myFunc"){ /* Don't forget me! */ }
        constructor{ /* and me! */ }
    }
}

Explicit generation

The codeBlock can also be generated explicitly

val codeBlock = createCodeBlock { /* indide the codeBlockBuilder */ }

Last updated