y
02/19/2024, 8:12 PM@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:
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?Youssef Shoaib [MOD]
02/19/2024, 8:27 PMy
02/19/2024, 8:45 PMa 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.Youssef Shoaib [MOD]
02/19/2024, 8:46 PMy
02/19/2024, 8:49 PM1.9.20
. and we have `data object`s in the code, so it has to be pretty new. (>= 1.8.20
, apparently)Youssef Shoaib [MOD]
02/19/2024, 8:50 PMYoussef Shoaib [MOD]
02/19/2024, 8:51 PMrequireSameSubclass
function in a different module? I think there's a known bug about contract casts not working well cross-moduley
02/19/2024, 9:03 PM