# Generic extension function and compilation checks

**URL:** <https://discuss.kotlinlang.org/t/generic-extension-function-and-compilation-checks/14422>\
**Category:** Support\
**Created:** [September 25, 2019, 12:58pm UTC](https://discuss.kotlinlang.org/t/generic-extension-function-and-compilation-checks/14422 "2019-09-25T12:58:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nico\_wk](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nico\_wk](https://discuss.kotlinlang.org/u/nico_wk)\
**Post date:** [September 25, 2019, 12:58pm UTC](https://discuss.kotlinlang.org/t/generic-extension-function-and-compilation-checks/14422/1 "2019-09-25T12:58:45Z")

</div>

Hello All,

sorry if my question was already asked but I could not find any answer.

I would like to have an extension function to extract the first element of a collection and coerce it to a specific subtype.

[code](https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS4zLjUwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsImpzQ29kZSI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiaW1wb3J0IGtvdGxpbi5yZWZsZWN0LktDbGFzc1xuaW1wb3J0IGtvdGxpbi5yZWZsZWN0LmZ1bGwuY2FzdFxuXG5vcGVuIGNsYXNzIEEge1xuICAgIG9wZW4gZnVuIHByaW50KCkge1xuICAgICAgICBwcmludGxuKFwidGhpcyBpcyBBXCIpXG4gICAgfVxufVxuY2xhc3MgQjogQSgpIHtcbiAgICBvdmVycmlkZSBmdW4gcHJpbnQoKSB7XG4gICAgICAgIHByaW50bG4oXCJ0aGlzIGlzIEJcIilcbiAgICB9XG59XG5jbGFzcyBDIHtcbiAgICBmdW4gcHJpbnQoKSB7XG4gICAgICAgIHByaW50bG4oXCJ0aGlzIGlzIENcIilcbiAgICB9XG59XG5cbkBTdXBwcmVzcyhcIlVOQ0hFQ0tFRF9DQVNUXCIpXG5mdW4gPFQ+IENvbGxlY3Rpb248Kj4ub2xkRmlyc3RBcyh0ZXN0RWxlbWVudDogVC4oKSAtPiBVbml0KSB7XG4gICAgYXNzZXJ0KCFpc0VtcHR5KCkpXG4gICAgdmFsIGZpcnN0ID0gZmlyc3QoKVxuICAgIGZpcnN0IGFzIFRcbiAgICBmaXJzdC50ZXN0RWxlbWVudCgpXG59XG5cbmZ1biA8VCwgWD4gQ29sbGVjdGlvbjxYPi5maXJzdEFzKGNsczogS0NsYXNzPFQ+LCB0ZXN0RWxlbWVudDogVC4oKSAtPiBVbml0KVxud2hlcmUgVDpYLFg6QW55IHtcbiAgICBhc3NlcnQoIWlzRW1wdHkoKSlcbiAgICB2YWwgZmlyc3QgPSBmaXJzdCgpXG4gICAgdmFsIGNhc3RlZCA9IGNscy5jYXN0KGZpcnN0KVxuICAgIGNhc3RlZC50ZXN0RWxlbWVudCgpXG59XG5cbmZ1biBtYWluKCkge1xuICAgIHZhbCBsaXN0IDpMaXN0PEE+ID0gbGlzdE9mKEIoKSlcbiAgICBcbiAgICAvLyBjb21waWxhdGlvbiBlcnJvciBleHBlY3RlZCAhXG4gICAgbGlzdC5maXJzdEFzKEM6OmNsYXNzKSB7XG4gICAgICAgIHByaW50KClcbiAgICB9XG59In0=)  
At first I had the function “oldFirstAs” but it is not good because I can pass anything as T. And I am forced to used this dirty @Suppress

Because I want compile time type safety I came with the second function “firstAs”

As you can see, the type T has an Upper Bound to X, the type of the collection. Why is the compiler letting me call with X=A and T=C, without raising an error ?

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [September 26, 2019, 6:03am UTC](https://discuss.kotlinlang.org/t/generic-extension-function-and-compilation-checks/14422/2 "2019-09-26T06:03:04Z")

</div>

Just as a B can be provided where an A is allowed, a `Collection<A>` can be provided where a `Collection<Any>` is allowed because `Collection` uses an “out” type parameter. The compiler finds that X=Any and T=C, works so it uses that.

Specifying the types explicitly produces the expected error.

```auto
list.firstAs<C, A>(C::class) {
        print()
    }

```

---

<div class="post-metadata">

**Author:** ![nico\_wk](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nico\_wk](https://discuss.kotlinlang.org/u/nico_wk)\
**Post date:** [September 27, 2019, 6:59am UTC](https://discuss.kotlinlang.org/t/generic-extension-function-and-compilation-checks/14422/3 "2019-09-27T06:59:36Z")

</div>

Thanks for the quick answer. I wanted to avoid passing both type arguments because the syntax is pretty verbose. I hoped that the compiler could get X type to the concrete receiver list.

Oh well, in this case I don’t need the KClass parameter and then I go back to my bad method with @suppress…  
If you can think of a clean workaround…
