0
# Type System
1
2
Comprehensive type representation and manipulation including type creation, variance handling, projection operations, and subtype relationships for advanced type-level programming.
3
4
## Capabilities
5
6
### Type Projections
7
8
Handle type variance and projections including star projections for generic type safety.
9
10
```kotlin { .api }
11
/**
12
* Represents a type projection with optional variance
13
*/
14
data class KTypeProjection(
15
/** The use-site variance specified in the projection, or null for star projection */
16
val variance: KVariance?,
17
/** The type specified in the projection, or null for star projection */
18
val type: KType?
19
) {
20
companion object {
21
/** Star projection, denoted by the * character */
22
val STAR: KTypeProjection
23
24
/** Creates an invariant projection of a given type */
25
fun invariant(type: KType): KTypeProjection
26
27
/** Creates a contravariant projection (in modifier) */
28
fun contravariant(type: KType): KTypeProjection
29
30
/** Creates a covariant projection (out modifier) */
31
fun covariant(type: KType): KTypeProjection
32
}
33
}
34
35
/**
36
* Represents type variance
37
*/
38
enum class KVariance {
39
INVARIANT, IN, OUT
40
}
41
```
42
43
**Usage Examples:**
44
45
```kotlin
46
// Create type projections
47
val stringType = typeOf<String>()
48
val invariantProjection = KTypeProjection.invariant(stringType)
49
val covariantProjection = KTypeProjection.covariant(stringType)
50
val contravariantProjection = KTypeProjection.contravariant(stringType)
51
val starProjection = KTypeProjection.STAR
52
53
// Inspect projections
54
println(covariantProjection.variance) // OUT
55
println(contravariantProjection.variance) // IN
56
println(starProjection.type) // null
57
58
// Use in type construction
59
val listClass = List::class
60
val projectedListType = listClass.createType(listOf(covariantProjection))
61
```
62
63
### Type Creation
64
65
Programmatically create KType instances with specific type arguments, nullability, and annotations.
66
67
```kotlin { .api }
68
/**
69
* Creates a KType instance with the given classifier, type arguments, and nullability
70
*/
71
fun KClassifier.createType(
72
arguments: List<KTypeProjection> = emptyList(),
73
nullable: Boolean = false,
74
annotations: List<Annotation> = emptyList()
75
): KType
76
77
/**
78
* Creates an instance of KType with the given classifier,
79
* substituting all type parameters with star projections
80
*/
81
val KClassifier.starProjectedType: KType
82
83
/**
84
* Returns a type corresponding to the given class with type parameters
85
* substituted as corresponding arguments
86
* @deprecated Use starProjectedType or createType() for clearer semantics
87
*/
88
@Deprecated("This function creates a type which rarely makes sense for generic classes")
89
val KClass<*>.defaultType: KType
90
```
91
92
**Usage Examples:**
93
94
```kotlin
95
// Create simple types
96
val stringClass = String::class
97
val stringType = stringClass.createType()
98
val nullableStringType = stringClass.createType(nullable = true)
99
100
// Create generic types
101
val listClass = List::class
102
val stringProjection = KTypeProjection.invariant(stringType)
103
val listOfStringType = listClass.createType(listOf(stringProjection))
104
105
// Create star projected types
106
val rawListType = listClass.starProjectedType // List<*>
107
108
// Inspect created types
109
println(nullableStringType.isMarkedNullable) // true
110
println(listOfStringType.arguments.size) // 1
111
```
112
113
### Type Operations
114
115
Perform advanced type operations including nullability changes and subtype checking.
116
117
```kotlin { .api }
118
/**
119
* Returns a new type with the same classifier and arguments but given nullability
120
*/
121
fun KType.withNullability(nullable: Boolean): KType
122
123
/**
124
* Returns true if this type is the same or is a subtype of other
125
*/
126
fun KType.isSubtypeOf(other: KType): Boolean
127
128
/**
129
* Returns true if this type is the same or is a supertype of other
130
*/
131
fun KType.isSupertypeOf(other: KType): Boolean
132
```
133
134
**Usage Examples:**
135
136
```kotlin
137
val stringType = typeOf<String>()
138
val nullableStringType = typeOf<String?>()
139
val anyType = typeOf<Any>()
140
141
// Change nullability
142
val madeNullable = stringType.withNullability(true)
143
val madeNonNull = nullableStringType.withNullability(false)
144
145
println(madeNullable.isMarkedNullable) // true
146
println(madeNonNull.isMarkedNullable) // false
147
148
// Check type relationships
149
println(stringType.isSubtypeOf(anyType)) // true
150
println(anyType.isSupertypeOf(stringType)) // true
151
println(stringType.isSubtypeOf(nullableStringType)) // true
152
```
153
154
### Type Inspection
155
156
Deep inspection of type structure including classifier access and argument analysis.
157
158
```kotlin { .api }
159
/**
160
* Access type components for detailed analysis
161
*/
162
interface KType {
163
/** The declaration of the classifier used in this type */
164
val classifier: KClassifier?
165
/** Type arguments passed for the classifier parameters */
166
val arguments: List<KTypeProjection>
167
/** True if this type was marked nullable in source */
168
val isMarkedNullable: Boolean
169
}
170
```
171
172
**Usage Examples:**
173
174
```kotlin
175
// Analyze complex generic types
176
val mapType = typeOf<Map<String, List<Int>>>()
177
178
// Inspect classifier
179
println(mapType.classifier) // interface kotlin.collections.Map
180
181
// Analyze type arguments
182
val typeArgs = mapType.arguments
183
println(typeArgs.size) // 2
184
185
// First argument (String)
186
val keyType = typeArgs[0].type
187
println(keyType?.classifier) // class kotlin.String
188
189
// Second argument (List<Int>)
190
val valueType = typeArgs[1].type
191
val valueArgs = valueType?.arguments
192
println(valueArgs?.size) // 1
193
println(valueArgs?.get(0)?.type?.classifier) // class kotlin.Int
194
195
// Check nullability at each level
196
println(mapType.isMarkedNullable) // false
197
println(keyType?.isMarkedNullable) // false
198
println(valueType?.isMarkedNullable) // false
199
```
200
201
### Type Parameter Information
202
203
Access information about type parameters including bounds and variance.
204
205
```kotlin { .api }
206
/**
207
* Represents a type parameter of a class or function
208
*/
209
interface KTypeParameter : KClassifier {
210
/** Name of the type parameter */
211
val name: String
212
/** Upper bounds of the type parameter */
213
val upperBounds: List<KType>
214
/** Variance of the type parameter */
215
val variance: KVariance
216
/** Whether the type parameter is reified */
217
val isReified: Boolean
218
}
219
```
220
221
**Usage Examples:**
222
223
```kotlin
224
// Access type parameters from a class
225
val listClass = List::class
226
val typeParams = listClass.typeParameters
227
228
if (typeParams.isNotEmpty()) {
229
val tParam = typeParams.first()
230
println(tParam.name) // "E"
231
println(tParam.variance) // OUT
232
println(tParam.upperBounds) // [kotlin.Any?]
233
println(tParam.isReified) // false
234
}
235
```