Why is the getOrElse() function of arrow-kt returning Any?
16:38 19 Nov 2025

I am trying to understand the getOrElse() function of arrow-kt. I want to create a simple function that takes a list of strings, filters them, and returns the first matching value as an Option< String >. I created the following trivial example:

fun filterStrings(incoming:List):Option
{
    val result = incoming.filter {
        it.contains('e')
    }.firstOrNone().getOrElse {
        incoming.filter {
            it.contains('b')
        }.firstOrNone()
    }

    return result
}

@Test
fun `test filter strings`() {
    val values = listOf("aad", "aba", "cdc", "bbb")
    val result = filterStrings(values)
    assertThat(result.isSome()).isTrue()
    assertThat(result.getOrBlank()).isEqualTo("aba")
}

In this example, the code will fail to compile since 'result' is being smartcast to Any. Why is the result variable being smartcast to Any instead of Options < String > ? Is there a way to work around this without doing an unsafe cast? I am aware that there are better ways to write a function like this, but I'm trying to get a handle on the fundamentals of what is happening.

java kotlin option-type arrow-kt