https://kotlinlang.org logo
Title
m

Mark

03/02/2023, 7:00 AM
Why does this give a
ClassCastException
(for the
first
of the
Pair
):
internal fun Pair<@Composable () -> String, (() -> Unit)?>.toButton(): @Composable () -> Unit = {
    var (label, onClick) = this@toButton // same runtime exception if use `this.first` instead
    ...
}
s

shikasd

03/04/2023, 2:17 PM
Can you paste the exception? Also looks like a bug, feel free to file an issue on tracker.
m

Mark

04/02/2023, 6:32 AM
I believe I’ve discovered the issue. I have a property declared like:
var positiveButton: (Pair<@Composable () -> String, T>)? = null
Usually, I assign it like:
positiveButton = @Composable { stringResource(id = labelResId) } to value
But sometimes like:
positiveButton = { label } to value
and that is what is causing the crash. Instead, I just need to assign like:
positiveButton = @Composable { label } to value
Surely this is a bug? Either the compiler should pick it up, or the runtime should allow it.
s

shikasd

04/02/2023, 4:14 PM
Yes, that's definitely a bug
m

Mark

04/03/2023, 7:13 AM
Can you reproduce it?
s

shikasd

04/03/2023, 7:17 AM
Didn't try to yet, but feel free to file a bug on the tracker :)
s

shikasd

04/03/2023, 6:16 PM
Just to leave a note here, I investigated this issue and seems like it is a limitation of compiler plugin in K1. It doesn't propagate the type of the receiver to the compiler plugin and in this case
{ label }
is a receiver for the
to
call. From what I see, it should be properly fixed in K2 inference, but cannot predict if that's the case atm.