• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Type Aliases
2============
3
4KotlinPoet provides API for creating Type Aliases, which supports simple class names, parameterized
5types and lambdas:
6
7```kotlin
8val k = TypeVariableName("K")
9val t = TypeVariableName("T")
10
11val fileTable = Map::class.asClassName()
12  .parameterizedBy(k, Set::class.parameterizedBy(File::class))
13
14val predicate = LambdaTypeName.get(
15  parameters = arrayOf(t),
16  returnType = Boolean::class.asClassName()
17)
18val helloWorld = FileSpec.builder("com.example", "HelloWorld")
19  .addTypeAlias(TypeAliasSpec.builder("Word", String::class).build())
20  .addTypeAlias(
21    TypeAliasSpec.builder("FileTable", fileTable)
22      .addTypeVariable(k)
23      .build()
24  )
25  .addTypeAlias(
26    TypeAliasSpec.builder("Predicate", predicate)
27      .addTypeVariable(t)
28      .build()
29  )
30  .build()
31```
32
33Which generates the following:
34
35```kotlin
36package com.example
37
38import java.io.File
39import kotlin.Boolean
40import kotlin.String
41import kotlin.collections.Map
42import kotlin.collections.Set
43
44typealias Word = String
45
46typealias FileTable<K> = Map<K, Set<File>>
47
48typealias Predicate<T> = (T) -> Boolean
49```
50