Ben Woodworth
11/13/2022, 9:57 PMacceptsString
I want to say R can be constrained to String by the smart cast but I'm not sure.
interface Accepts<in T> {
fun accept(value: T)
}
object AcceptsString : Accepts<String> {
override fun accept(value: String) = println(value)
}
fun <R> example(providesR: () -> R, acceptsR: Accepts<R>) {
if (acceptsR is AcceptsString) { // so R must be String
val acceptsString: AcceptsString = acceptsR
val providedR = providesR()
acceptsString.accept(providedR)
// ^^^^^^^^^ Type mismatch.
// Required: String
// Found: R
}
}Dominaezzz
11/13/2022, 11:22 PMDominaezzz
11/13/2022, 11:22 PMAcceptsString can't take R .Ben Woodworth
11/13/2022, 11:38 PMacceptsString.accept(providedR as String) in this case.
(And if so, could Kotlin in the future be made to infer R as String here)Dominaezzz
11/14/2022, 12:15 AMR is not a CharSequence an exception will be thrown if you do that.Dominaezzz
11/14/2022, 12:16 AMBen Woodworth
11/14/2022, 12:38 AMCharSequence types back to String in the example after playing around, so fixed that in a code block)Ben Woodworth
11/14/2022, 12:40 AMIfThat's the thing, I don't think it's possible foris not aRan exception will be thrown if you do that.String
R to be anything other than a String within that if blockBen Woodworth
11/14/2022, 12:41 AM: AcceptsString is proof of thatDominaezzz
11/14/2022, 8:37 AM