TypeDefs

A type often called typedef is an alias which can be used to define a custom type. It uses the `typedef` keyword which is also known in other languages like C or C++. They can have parameters but the usage is not required. To create a new typedef for the generation `TypeDefSpec` class.

TypeDefSpec.builder("json")
    // Import the DYNAMIC constant from net.theevilreaper.dartpoet.type.DYNAMIC
    .returns(Map::class.parameterizedBy(String::class.asTypeName(), DYNAMIC))
    .build()

Generics:

A typedef can also use generic types to adapt the code to different objects used within it. You can pass your generic types as TypeName in the builder method as an array value.

val genericClassName = ClassName("E")
val secondGenericClassName = ClassName("T")

TypeDefSpec.builder(
    "DoubleValueUpdate",
    genericClassName, secondGenericClassName
)
    .name("Function")
    .parameters(
        ParameterSpec.builder("first", genericClassName)
            .nullable(true)
            .build(),
        ParameterSpec.builder("second", secondGenericClassName)
            .nullable(true)
            .build()
    )
    .build(),

circle-exclamation

Last updated