From d3cf1ab854297d2940a74464a55eccc3efbd4f43 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Sun, 31 Jul 2022 14:54:46 +0200 Subject: [PATCH] Document inline classes with generic underlying parameter (#313) --- proposals/inline-classes.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/proposals/inline-classes.md b/proposals/inline-classes.md index abfbbc6e0..ed5c9eeb8 100644 --- a/proposals/inline-classes.md +++ b/proposals/inline-classes.md @@ -352,6 +352,8 @@ This is needed to preserve information about inline classes at runtime. #### Generic inline class mapping +_Note: this functionality is added in Kotlin 1.8._ + Consider the following sample: ```kotlin @JvmInline @@ -370,7 +372,21 @@ value class GenericArray(val y: Array) fun foo(g: GenericArray) {} // `g` has type `Integer[]` or `Object[]`? ``` -Therefore, because of this ambiguity, such cases are going to be forbidden in the first version of inline classes. +Therefore, because of this ambiguity, such cases were forbidden in the first version of inline classes. + +However, since 1.8 these inline classes are mapped to the upper bound of the generic parameter. +In other words, given the code +```kotlin +@JvmInline +value class IC(val a: T) + +fun foo(ic: IC) +``` +the compiler generates the following +```kotlin +fun foo-(ic: Any?) +``` +since `T`'s upper bound is `Any?`. * Sidenote: maybe it's worth to consider inline classes with reified generics: ```kotlin