NameAllocator

constructor()


constructor(preallocateKeywords: Boolean)

Parameters

preallocateKeywords

If true, all Kotlin keywords will be preallocated. Requested names which collide with keywords will be suffixed with underscores to avoid being used as identifiers:

val nameAllocator = NameAllocator(preallocateKeywords = true)
println(nameAllocator.newName("when")) // prints "when_"

If false, keywords will not get any special treatment:

val nameAllocator = NameAllocator(preallocateKeywords = false)
println(nameAllocator.newName("when")) // prints "when"

Note that you can use the %N placeholder when emitting a name produced by NameAllocator to ensure it's properly escaped for use as an identifier:

val nameAllocator = NameAllocator(preallocateKeywords = false)
println(CodeBlock.of("%N", nameAllocator.newName("when"))) // prints "`when`"

The default behaviour of NameAllocator is to preallocate keywords - this is the behaviour you'll get when using the no-arg constructor.