Skip to content

Commit 49ba391

Browse files
authored
Merge pull request #10229 from igfoo/igfoo/singleOrNullSubType
Kotlin: Implement and use singleOrNullSubType
2 parents 99bd6f1 + 6f82b06 commit 49ba391

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ open class KotlinUsesExtractor(
11691169
"kotlin.Boolean", "kotlin.Byte", "kotlin.Char", "kotlin.Double", "kotlin.Float", "kotlin.Int", "kotlin.Long", "kotlin.Number", "kotlin.Short"
11701170
)
11711171

1172-
private fun kotlinFunctionToJavaEquivalent(f: IrFunction, noReplace: Boolean) =
1172+
private fun kotlinFunctionToJavaEquivalent(f: IrFunction, noReplace: Boolean): IrFunction =
11731173
if (noReplace)
11741174
f
11751175
else
@@ -1186,8 +1186,7 @@ open class KotlinUsesExtractor(
11861186
decl.valueParameters.zip(f.valueParameters).all { p -> p.first.type.classifierOrNull == p.second.type.classifierOrNull }
11871187
} ?:
11881188
// Or if there is none, look for the only viable overload
1189-
javaClass.declarations.singleOrNull { decl ->
1190-
decl is IrFunction &&
1189+
javaClass.declarations.singleOrNullSubType<IrFunction> { decl ->
11911190
decl.name == f.name &&
11921191
decl.valueParameters.size == f.valueParameters.size
11931192
} ?:
@@ -1213,7 +1212,7 @@ open class KotlinUsesExtractor(
12131212
else
12141213
null
12151214
}
1216-
} as IrFunction? ?: f
1215+
} ?: f
12171216

12181217
fun <T: DbCallable> useFunction(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>? = null, noReplace: Boolean = false): Label<out T> {
12191218
return useFunction(f, null, classTypeArgsIncludingOuterClasses, noReplace)

java/kotlin-extractor/src/main/kotlin/utils/Iterable.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ inline fun <reified S: IrDeclaration> Iterable<IrDeclaration>.findSubType(
1313
return this.find { it is S && predicate(it) } as S?
1414
}
1515

16+
/**
17+
* This behaves the same as Iterable<IrDeclaration>.singleOrNull, but
18+
* requires that the value found is of the subtype S, and it casts
19+
* the result for you appropriately.
20+
*/
21+
inline fun <reified S: IrDeclaration> Iterable<IrDeclaration>.singleOrNullSubType(
22+
predicate: (S) -> Boolean
23+
): S? {
24+
return this.singleOrNull { it is S && predicate(it) } as S?
25+
}
26+

0 commit comments

Comments
 (0)