One thing i’ve noticed after upgrading from compos...
# compose
m
One thing i’ve noticed after upgrading from compose 1.1.1 to 1.2.1 (along with a kotlin 1.7.10 bump) is that I can no longer do this:
Copy code
val screens = listOfScreens.sortedBy {
            stringResource(it.label)
        }
it complains that i can’t execute the stringResource function outside of composable scope. Instead i have to do this:
Copy code
val context = LocalContext.current
        val screens = listOfScreens.sortedBy {
            context.getString(it.label)
        }
I’m a little confused what has changed between either kotlin or compose that this became a compile error
m
sortBy
is an inline function, so it should probably work. Might be a bug in the compose compiler. Although you really don’t want to do sorting like that in a compose function. Sorting is one of the examples of using the
derivedStateOf
function. Although since Compose doesn’t treat list as immutable that might not help.
Actually I think what I remember from the Google talk was that you should ideally move sorting up to the view model.
m
sortedBy
selector lambda parameter is marked as
crossinline
which effectively prevents you from using non-local returns. not sure what kotlin version exactly introduced this change, you can go through release notes Or commits to confirm it was introduced in a recent version
m
My upgrade was this: Kotlin: 1.6.10 -> 1.7.10 Compose Compiler: 1.1.1 -> 1.3.0 Compose Runtime: 1.2.1 It’s obviously not the runtime, so it’s either kotlin or compose. The issue i’m having is that the sorting is taking a string resource id, pulling it in for the current language, and then sorting by that. It’s a perfectly valid use case IMHO. Like i said in my post, there’s a workaround by pulling the context out of LocalContext. It was just a behavioral change i wasn’t expecting.