I've got this function ```@OptIn(ExperimentalContr...
# getting-started
y
I've got this function
Copy code
@OptIn(ExperimentalContracts::class)
inline fun <T, reified S : T> requireSameSubclass(a: T, b: T) {
    contract {
        returns() implies (a is S && b is S)
    }

    if (a !is S || b !is S) {
        throw IllegalArgumentException("uh oh")
    }
}
and when I attempt to use it like so:
Copy code
val comparator: Comparator<Bar> = Comparator { a: Foo, b: Foo -> 
    requireSameSubclass<Foo, Bar>(a, b)
    a.functionOfBar().compareTo(b.functionOfBar())
}
a
and
b
appear to smart-cast to
Bar
on IntelliJ, but then compilation fails, thinking that
a
and
b
are
Foo
. is this a bug?
y
Compiles just fine for me on the playground
y
strange. and adding
Copy code
a as Bar
b as Bar
after the require, makes it compile (and IntelliJ detects it as a useless cast). I guess I gotta go track down the anomaly in my company's build setup.
y
Might be an outdated Kotlin version or something. IntelliJ's Kotlin plugin might not be the same exact version as your compiling Kotlin version
y
that was my initial thought, but it appears to be
1.9.20
. and we have `data object`s in the code, so it has to be pretty new. (>=
1.8.20
, apparently)
y
What is the exact error message you're getting?
Also, is the
requireSameSubclass
function in a different module? I think there's a known bug about contract casts not working well cross-module
❤️ 1
y
wow, I think that's it. it compiles now.