What is the purpose of the keyword "by" (property delegates) when declaring a MutableState<T> with remember?

28 views Asked by At

Ok. So while I was working to my current Android Jetpack Compose project, I managed to get stuck on the following concept related to property delegates, more exactly the keyword "by". I've already read this post on stackoverflow which was pretty well received:

Difference between "remember" and "MutableState" in JetpackCompose

Here it says:

enter image description here

I've also looked on some other sites trying to better understand the topic:

kotlin delegated properties

remember mutableStateOf a cheatsheet

Finally, all I want to understand is if there is a difference between these 2 declarations or they do the same thing:

val showA: MutableState<Boolean> = remember { mutableStateOf(true) }

and

val showA: MutableState<Boolean> by remember {mutableStateOf(true)}

There are plenty of resources that talk about this subject, but I think a summarizing answer would be of big use. So, what is the role of using "by" in such constructs and why does Android Studio throws the following error when using the second code sample making the Alt + Enter option for importing not work:

Property delegate must have a 'getValue(Nothing?, KProperty*>)' method. None of the following functions are suitable. Import operator State.getValue()

Later update:

I've found this question which answers almost completely my question:

Difference between "remember" and "by remember"

What I still don't know is why I get the above error when using "by remember" and trying to import the setter and getter?

0

There are 0 answers