Name Allocator
constructor()
Parameters
preallocate Keywords
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_"
Content copied to clipboard
If false, keywords will not get any special treatment:
val nameAllocator = NameAllocator(preallocateKeywords = false)
println(nameAllocator.newName("when")) // prints "when"
Content copied to clipboard
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`"
Content copied to clipboard
The default behaviour of NameAllocator is to preallocate keywords - this is the behaviour you'll get when using the no-arg constructor.