Dmitry Akishin
10/26/2022, 5:30 AMremember
them? Or is there some optimization already built in?
Here is some specific code:
@Composable
private fun SomeComposable() {
val fun1: (int: Int) -> String = { int ->
int.toString()
}
fun fun2(int: Int): String {
return int.toString()
}
val fun3: (int: Int) -> String = remember {
{ int ->
int.toString()
}
}
fun1(5)
fun2(5)
fun3(5)
}
What would be the most optimal approach between fun1
, fun2
and fun3
?andylamax
10/26/2022, 7:11 AMAlbert Chang
10/26/2022, 7:21 AMDmitry Akishin
10/26/2022, 7:47 AMDmitry Akishin
10/26/2022, 8:56 AM@Composable
private fun SomeComposable(param: Int) {
val fun1: () -> String = {
param.toString()
}
fun fun2(): String {
return param.toString()
}
val fun3: () -> String = remember {
{
param.toString()
}
}
fun1()
fun2()
fun3()
}
In this case fun1 and fun2 are totally ok, is that correct?Albert Chang
10/26/2022, 9:22 AMparam
changes, the fun2
instance will be recreated. Also note that fun3
is broken because it only captures the initial param
value even if param
changes.andylamax
10/26/2022, 9:38 AMval fun1: (int: Int) -> String = { int ->
int.toString()
}
fun fun2(int: Int): String {
return int.toString()
}
val fun3: (int: Int) -> String = remember {
{ int ->
int.toString()
}
}
@Composable
private fun SomeComposable() {
fun1(5)
fun2(5)
fun3(5)
}
See??Dmitry Akishin
10/26/2022, 9:49 AMandylamax
10/26/2022, 9:50 AMandylamax
10/26/2022, 9:53 AMDmitry Akishin
10/26/2022, 9:53 AMremember(param)
Dmitry Akishin
10/26/2022, 9:54 AMAlbert Chang
10/26/2022, 9:55 AMandylamax
10/26/2022, 9:57 AMfun3
is bad coz it is capturing an old value and it kinda goes off sync when param changes and might leak memory
fun1
would behave exactly like fun2
(atleast on jvm), but fun2
is less verbose (which makes it the best candidate)andylamax
10/26/2022, 10:00 AM@Composable
fun Fun4(param: Int) {
param.toString()
}
@Composable
private fun SomeComposable(param: Int) {
Fun4(param)
}
Compose will take care of either recomposing it (if param changes) or not. You won't have to worry at allDmitry Akishin
10/26/2022, 10:07 AMvar param by remember {
mutableStateOf(0)
}
val fun1: () -> String = {
param.toString()
}
param = 5
fun1()
fun1()
will be 0 and then 5, correct?Albert Chang
10/26/2022, 10:10 AMAlbert Chang
10/26/2022, 10:13 AMthe same thing
?Dmitry Akishin
10/26/2022, 10:13 AMDmitry Akishin
10/26/2022, 10:14 AMAlbert Chang
10/26/2022, 10:15 AMMutableState
, which will never change.andylamax
10/26/2022, 10:15 AMDmitry Akishin
10/26/2022, 10:17 AMby remember
), like in the example?andylamax
10/26/2022, 10:18 AMMutableState
, then yes, Fun4
will be less performant. But if it would be capturing parameters (as in the examples), it will be more performant that its capturing counterpartsDmitry Akishin
10/26/2022, 10:26 AMval fun1: @Composable () -> String = {
param.toString()
}
Albert Chang
10/26/2022, 10:27 AMAlbert Chang
10/26/2022, 10:28 AMDmitry Akishin
10/26/2022, 10:29 AMfun1: () -> String
will work as expected then?Dmitry Akishin
10/26/2022, 10:35 AM@Composable
fun SomeComposable(param: String) {
@Composable
fun fun5() {
Text(text = param)
}
val fun6: @Composable () -> Unit = {
Text(text = param)
}
fun5()
fun6()
fun7(param)
}
@Composable
fun fun7(param: String) {
Text(text = param)
}
Is there any significant penalty when using fun5/fun6 intead of fun7? Or it is the same as for normal functions, so pretty much no difference?Albert Chang
10/26/2022, 12:13 PMDmitry Akishin
10/26/2022, 12:16 PM