Skip to content

Conversation

@Komdosh
Copy link

@Komdosh Komdosh commented Oct 18, 2025

There is a find and filterIs methods for iterables, but there is no method for finding one specific element in iterable.

For example:

val services = listOf<CommonInterface>(specificInterfaceA, specificInterfaceB, specificInterfaceC)

val specificService = services.findIs<SpecificInterfaceB>()

it is easy to implement, but it is still not in stdlib.

public inline fun <reified R> Iterable<*>.findIs(): R? {
    return find { element -> element is R } as R?
}

Forgotten for 11 years actually: KT-6300

@Komdosh Komdosh changed the title KT-6300: Stdlib: add findIs and firstIs method [KT-6300]: Stdlib: add findIs and firstIs method Oct 18, 2025
@Komdosh Komdosh requested a review from a team as a code owner October 20, 2025 15:55
@Komdosh
Copy link
Author

Komdosh commented Oct 29, 2025

Any issues, suggestions?

@MukjepScarlet
Copy link

Shouldn't this
public inline fun <reified R> Iterable<*>.firstIs(): R? {
be
public inline fun <reified R> Iterable<*>.firstIs(): R {
?
Because it throws exception if no instance of R found.

@MukjepScarlet
Copy link

By the way Sequence also needs this.

@Komdosh
Copy link
Author

Komdosh commented Nov 4, 2025

Shouldn't this public inline fun <reified R> Iterable<*>.firstIs(): R? { be public inline fun <reified R> Iterable<*>.firstIs(): R { ? Because it throws exception if no instance of R found.

Fixed, thanks!

@Komdosh
Copy link
Author

Komdosh commented Nov 5, 2025

This was rejected

@Komdosh Komdosh closed this Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants