-
Notifications
You must be signed in to change notification settings - Fork 302
Open
Milestone
Description
(WIP, not ready for review yet)
In 1.0, our TypeName
modeling is largely based on JavaPoet's. This mostly works, but falls short in some ways that don't map well onto Kotlin's own semantics.
Problems:
WildcardTypeName
extendsTypeName
, whereas Kotlin offersKTypeProjection
and it is not a subtype- Definitely not null types, which don't have an easy spot in the current system (see Support definitely non-nullable types #1268)
TypeName
is currentlysealed
, butKType
is not and I think it's clear they want to keep the option open- There is currently no support for intersection types
ParameterizedTypeName
extendsTypeName
, but Kotlin doesn't differentiate these. KType contains a classifier and arguments.- Semantics do not currently line up
Proposal:
- Replace
WildcardTypeName
with a projection type that does not extendTypeName
.
class `KpTypeProjection(val variance: KModifier?, val type: TypeName?)`
- Model
TypeName
more likeKType
, inline awayParameterizedTypeName
.
interface KpType {
val classifier: KpClassifier // KpClass or KpTypeParameter
val arguments: List<KpTypeProjection>
val isMarkedNullable: Boolean
}
IntroduceIntersectionTypeName
for intersection types. This should also cover definitely non-nullable types- Introduce
upperBounds
toKpTypeParameter
, allowing support for intersection types - Introduce
KType.makeNotNull()
, which returns a special-caseKpType
that adds this extra notation. This matches how Kotlin handles it internally, as it's just a flag on the proto. - Make
TypeName
no longer sealed. - Rename to match Kotlin semantics
Before | After |
---|---|
KpClassifier | |
TypeName | KpType |
ClassName | KpClass |
TypeVariableName | KpTypeParameter |
WildcardTypeName | KpTypeProjection |
Then common types could be implemented as such
KpClass
kpTypeOf<String>() // Returns KpType
String::class.kp // Returns KpClass
String::class.kpClassifier // Returns KpClassifier
String::class.kpType // Returns KpType
Generics
kpTypeOf<List<String>>() // Returns KpType
List::class.parameterizedBy(String::class) // Returns KpType
List::class.parameterizedBy(String::class.kp) // Returns KpType
List::class.parameterizedBy(String::class.kpType) // Returns KpType
List::class.parameterizedBy(KpTypeProjection<String>(OUT)) // Returns KpType
Intersection types
// Renders to T : Any, T : Runnable
KpTypeParameter("T", upperBounds = listOf(ANY, kpTypeOf<Runnable>())
// Other params include variance and isReified
Metadata
Metadata
Assignees
Labels
No labels