Reflection: add KTypeProjection and KType.arguments

#KT-8998 In Progress
This commit is contained in:
Alexander Udalov
2016-07-12 16:20:44 +03:00
parent c1dd831e65
commit f69cc01f8e
7 changed files with 246 additions and 5 deletions
+52 -1
View File
@@ -29,6 +29,12 @@ public interface KType {
*/
public val classifier: KClassifier?
/**
* Type arguments passed for the parameters of the classifier in this type.
* For example, in the type `Array<out Number>` the only type argument is `out Number`.
*/
public val arguments: List<KTypeProjection>
/**
* `true` if this type was marked nullable in the source code.
*
@@ -42,9 +48,54 @@ public interface KType {
*
* ```
* fun <T> foo(t: T) {
* // isMarkedNullable == false for t's type, but t can be null here
* // isMarkedNullable == false for t's type, but t can be null here when T = "Any?"
* }
* ```
*/
public val isMarkedNullable: Boolean
}
/**
* Represents a type projection. Type projection is usually the argument to another type in a type usage.
* For example, in the type `Array<out Number>`, `out Number` is the covariant projection of the type represented by the class `Number`.
*
* Type projection is either the star projection, or an entity consisting of a specific type plus optional variance.
*
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#type-projections)
* for more information.
*/
public sealed class KTypeProjection {
/**
* The type specified in the projection, or `null` if this is a star projection.
*/
public abstract val type: KType?
/**
* Invariant projection of a type. Invariant projection is just the type itself, without any use-site variance modifiers applied to it.
* For example, in the type `Set<String>`, `String` is an invariant projection of the type represented by the class `String`.
*/
public data class Invariant(override val type: KType) : KTypeProjection()
/**
* Contravariant projection of a type, denoted by the `in` modifier applied to a type.
* For example, in the type `MutableList<in Number>`, `in Number` is a contravariant projection of the type of class `Number`.
*/
public data class In(override val type: KType) : KTypeProjection()
/**
* Covariant projection of a type, denoted by the `out` modifier applied to a type.
* For example, in the type `Array<out Number>`, `out Number` is a covariant projection of the type of class `Number`.
*/
public data class Out(override val type: KType) : KTypeProjection()
/**
* Star projection, denoted by the `*` character.
* For example, in the type `KClass<*>`, `*` is the star projection.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/generics.html#star-projections)
* for more information.
*/
public object Star : KTypeProjection() {
override val type: KType?
get() = null
}
}