https://kotlinlang.org logo
m

Michal Bacik

10/30/2019, 7:59 PM
Please help - how can I make "local state" of widget that reflects to change and rebuilds the widget when changed? Here I want to change color of rectangle after click on it. But it's not rebuilt. I suppose I don't need global
@Model
class for this scenario.
Copy code
@Composable
fun MyRect(){
    var color = +memo{ Color.Green }
    Clickable(onClick = {
        color = Color.Red
    }) {
        ColoredRect(brush = SolidColor(color), height = 100.dp)
    }
}
l

Luca Nicoletti

10/30/2019, 8:01 PM
val color = +state{ Color.Green }
works
you then need to unwrap the state with
color.value
when needed
g

Grigorii Yurkov

10/30/2019, 8:02 PM
var color by +state { Color.Green }
l

Luca Nicoletti

10/30/2019, 8:02 PM
val*
m

Michal Bacik

10/30/2019, 8:03 PM
Perfect, works. Thanks! I've seen
memo
somewhere, but that's probably for different thing.
g

Grigorii Yurkov

10/30/2019, 8:03 PM
if it will be val, how can you change it
l

Luca Nicoletti

10/30/2019, 8:03 PM
You change
color.value
m

Michal Bacik

10/30/2019, 8:03 PM
it's
val
actually. you change its
value
.
l

Luca Nicoletti

10/30/2019, 8:03 PM
not
color
itself
g

Grigorii Yurkov

10/30/2019, 8:04 PM
i wrote
by
, not
=
m

Michal Bacik

10/30/2019, 8:05 PM
Hmm. There are two variants.
var color by +state{ Color.Green }
looks nicer, it doesn't require the
.value
thing.
g

Grigorii Yurkov

10/30/2019, 8:06 PM
yeah, i've seen it in sources
l

Luca Nicoletti

10/30/2019, 8:07 PM
Yeah, looks better, as they;re also planning to get rid of
+
m

Michal Bacik

10/30/2019, 8:08 PM
And who knows for what is
memo
? 🤔
l

Luca Nicoletti

10/30/2019, 8:09 PM
It’s an
Effect
l

Leland Richardson [G]

10/30/2019, 8:11 PM
both
val color = +state { ... }
and
var color by +state { ... }
will work.
we may at some point lint against
var
in a composable function that is not introduced by a property delegate. not sure yet though
i prefer the
var color by ...
version for a couple of reasons, but some people might consider that more magic, so up to you
👌 1
l

Luca Nicoletti

10/30/2019, 8:12 PM
It looks nicer indeed
f

Fudge

10/30/2019, 8:24 PM
I say to get rid of the other way
Having multiple ways of doing the same thing is just confusing
m

Michal Bacik

10/30/2019, 8:27 PM
Proper way is
var ... by
. You're interested in your variable, not the delegate that is behind providing its value.
👆 1
f

Fudge

10/30/2019, 8:29 PM
and also wars about the right way of doing it are not nice
l

Leland Richardson [G]

10/30/2019, 8:35 PM
it’s definitely something worth considering
in general i agree with “multiple ways of doing the same thing is bad”
but it’s hard to know where you draw the line here….
Copy code
val x = a
val y = x.first
val z = x.second
vs
Copy code
val (y, z) = a
should kotlin make the 1st way impossible?
l

Luca Nicoletti

10/30/2019, 8:38 PM
YES
🧌 3
f

Fudge

10/31/2019, 9:23 AM
Hmm, I wrote a long message about why that's not the same thing, but that got deleted because of internet connectivity. Anyway, the conclusion is that at the very least there should be an inspection to use the
by
syntax and to only use the
by
syntax in official examples.
l

Luca Nicoletti

10/31/2019, 9:24 AM
Why? What if I need a
State
object?
f

Fudge

10/31/2019, 9:25 AM
Why would you need a
State
object?
l

Luca Nicoletti

10/31/2019, 9:29 AM
Maybe I want
component1
and
component2
f

Fudge

10/31/2019, 9:29 AM
Maybe
But why would you need a State object?
l

Luca Nicoletti

10/31/2019, 9:30 AM
If you don’t have the state object you can’t get
compoent1
and
component2
of
State
Btw, I already said the
by
solution looks cleaner and I’d use that, but as @Leland Richardson [G] said it’s hard to know where to force something
f

Fudge

10/31/2019, 9:31 AM
oh, you mean
(someState,setSomeState)
l

Luca Nicoletti

10/31/2019, 9:31 AM
yes
f

Fudge

10/31/2019, 9:31 AM
Why would you use this syntax over using
by
?
Right now it seems to me like that should begone too
g

Grigorii Yurkov

10/31/2019, 9:32 AM
Copy code
val state = +state { false }
Checkbox(checked = state.value, onCheckedChange =    state.component2())
var checked by state
f

Fudge

10/31/2019, 9:32 AM
```
That's... unreadable
I don't think that's desired
I would rather read
Copy code
var state by state { false }
Checkbox(checked = state.value, onCheckedChange  = {state = it})
But I could see
Copy code
val (state,setState) = state { false }
Checkbox(checked = state.value, onCheckedChange =    setState)
But is this tiny synctactic sugar really worth having 3 ways of doing the same thing?
l

Luca Nicoletti

10/31/2019, 9:38 AM
Well that’s 2 ways of doing the same 😛
f

Fudge

10/31/2019, 9:40 AM
What do you mean? You can 1) do
Copy code
val (value,setValue)  = state { false }
2)
Copy code
val value = state {false}
3)
Copy code
var value by state { false }
l

Luca Nicoletti

10/31/2019, 9:40 AM
Yeah, the second one is bundle into the first
What I meant was:
= +state{}
could be used for
1.
,
by
could not. Maybe that’s the reason for having both of them 🙂
a

andrew

11/07/2019, 12:19 AM
the first one is a lot like react hooks
l

Luca Nicoletti

11/07/2019, 4:01 AM
Well that’s because Compose is influenced by React as well, alongside by Flutter, Vue.js, Litho