Just started having a look at Jetback compose.... ...
# compose
v
Just started having a look at Jetback compose.... decided it's not for me when I saw a variable declaration as `
Copy code
val count = remember { mutableStateOf(0) }
I mean, what does that even mean? What's wrong with
var count = 0
or
val count: IntWrapper = IntWrapper(0)
.
remember
isn't listed as in import, control-clicking doesn't take me to a declaration. It's wilfully obtuse!
a
I suggest you read the documentation here: https://developer.android.com/jetpack/compose/mental-model
After reading those, youll understand what those lines are for and why you need them.
v
Thank you, I've taken a read. I get the gist of what it's doing, but I almost long for the verbosity of Java -
MutableState<Int> count = StateManager.add<Int>(0)
or something like that (I just invented it) would be more explicit to me. I've been trying to persuade a C++ developer friend of mine to look at Kotlin and this sort of stuff is putting him off. It's a pity that the
by remember
delegate suggested in the documentation doesn't work for Desktop.
Just found https://github.com/JetBrains/compose-jb/issues/142 so it's not just me who has struggled 🙂
a
so you were working with desktop compose? i suggest you join the dedicated slack channel for desktop compose.. it is expected to have these issues for now as compose and even more so compose desktop is still very new.. those would be fixed eventually.. so for now lets keep providing feedback and reporting issues to contribute
z
You are free to use fully explicit types if you wish. Eg
Copy code
val count: MutableState<Int> = remember<MutableState<Int>> { mutableStateOf<Int>(0) }
Although after seeing this pattern a couple of times you probably only need one of those.
c
@Zach Klippenstein (he/him) [MOD] ooh. That is kinda nice to see it "fully" written out. I wonder if intellij can collapse that for you to the simiplified version and vice versa. I'll try that out the next time I'm near an ide.
z
If you type that in IntelliJ/AS, it will warn you that all the explicit types are unnecessary and give you a quick fix to remove them. It will also probably give you a quick fix to add the type to the val I believe, but probably not the other ones (it’s probably most useful on the val, since at the end of the day you really care the most about what type the val is). But for the OP, I think you should be able to turn that unnecessary explicit types warning off.
👍 1
v
I am fine with explicit types being turned off normally. I make a judgement call in my own code whether they add clarity or not. But when learning a new tool or framework, I'd err on the side of explicit. And remember made me immediately wonder if there was a forget option as well.
z
“Forget” is the default – just like in regular code, variables declared in a function will never last beyond a single invocation of that function.