Variable

The bridge between ParameterSpec and PropertySpec.

The goal of Variable

ParameterSpec and PropertySpec are very similar:

ParameterSpec

PropertySpec

name

name

modifiers

modifiers

type

type

defaultValue

initializer

null

mutable: Boolean

Although they have more fields, these are the ones KotlinPoetDSL supports before V0.2.

Because they are this similar it would make sense to pass around a type that has the commonalities of both the classes. In KotlinPoetDSL, this class is called Variable.

Constructions

There are 3 function-names: of, valOf, varOf. These can all be invoked the same way. Therefor I show below only using valOf.

val clazz = ClassName("", "Clazz")

"a" valOf String::class // val a : String
"a" valOf clazz //val  a: Clazz

// S surrounds string with quotes
"a".valOf<String>("Hi %L".S(), "you") // val a : String = "Hi you"
"a".valOf(clazz, "%T()", clazz)      // a: Clazz = Clazz()

 // will be supported in V0.2
//"a".valOf(String::class, "Hi %L", "you") // val a : String = "Hi you"

for the vararg:

Interoperability

Slightly interoperable, but will be improved in V0.2

Creating Variables

Using Variable

In V0.1 the variables can't be used. In V0.2, the variables can be used interchangeable with ParamSpec and PropertySpec.

Getting KotlinPoetTypes

Last updated

Was this helpful?