# "Materializing" a CapturedType without using an intermediate method

**URL:** <https://discuss.kotlinlang.org/t/materializing-a-capturedtype-without-using-an-intermediate-method/23834>\
**Category:** Language Design\
**Created:** [January 3, 2022, 3:21pm UTC](https://discuss.kotlinlang.org/t/materializing-a-capturedtype-without-using-an-intermediate-method/23834 "2022-01-03T15:21:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![liautaud](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/liautaud/32/9069_2.png) [@liautaud](https://discuss.kotlinlang.org/u/liautaud)\
**Post date:** [January 3, 2022, 3:21pm UTC](https://discuss.kotlinlang.org/t/materializing-a-capturedtype-without-using-an-intermediate-method/23834/1 "2022-01-03T15:21:35Z")

</div>

The following code will not type-check using the latest Kotlin compiler.

```auto
class GenericA<T>
class GenericB<T>

fun instance(): GenericA<*> = TODO()
fun <T> transformGeneric(a: GenericA<T>): GenericB<T> = TODO()
fun <T> reconcileGenerics(a: GenericA<T>, b: GenericB<T>): Nothing = TODO()

fun withoutIntermediateMethod() {
    val a = instance()
    val b = transformGeneric(a)

    // Type mismatch: inferred type is Any? but CapturedType(*) was expected
    // Type mismatch: inferred type is Any? but CapturedType(out Any?) was expected
    reconcileGenerics(a, b)
}

```

However, we can make it type-check by “materializing” the `CapturedType<*>` using an intermediate method with a new type parameter `T`.

```auto
fun withIntermediateMethod() {
    fun <T> reconcileTyped(a: GenericA<T>) {
        val b = transformGeneric(a)
	    reconcileGenerics(a, b)
    }
 	
    val a = instance()
    reconcileTyped(a)
}

```

Is there any way to get this to work without using an intermediary method, and ideally without changing the signature of `reconcileGenerics`? Thanks!

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [January 3, 2022, 7:56pm UTC](https://discuss.kotlinlang.org/t/materializing-a-capturedtype-without-using-an-intermediate-method/23834/2 "2022-01-03T19:56:28Z")

</div>

This workaround works as expected:

```run-kotlin
class GenericA<T>
class GenericB<T>

fun instance(): GenericA<*> = TODO()
fun <T> transformGeneric(a: GenericA<T>): GenericB<T> = TODO()
fun <T> reconcileGenerics(a: GenericA<T>, b: GenericB<T>): Nothing = TODO()

fun main() {
    val a = instance() as GenericA<Any>
    val b = transformGeneric(a)

    // Type mismatch: inferred type is Any? but CapturedType(*) was expected
    // Type mismatch: inferred type is Any? but CapturedType(out Any?) was expected
    reconcileGenerics(a, b)
}

```

---

<div class="post-metadata">

**Author:** ![liautaud](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/liautaud/32/9069_2.png) [@liautaud](https://discuss.kotlinlang.org/u/liautaud)\
**Post date:** [January 3, 2022, 10:35pm UTC](https://discuss.kotlinlang.org/t/materializing-a-capturedtype-without-using-an-intermediate-method/23834/3 "2022-01-03T22:35:31Z")

</div>

Thanks! This seems to work well, except for the fact that the type-checker complains about the unchecked cast.
