Andrew Hughes
08/23/2022, 4:54 PMColorScheme
is annotated with @Stable
, however, it does not override equals
. The @Stable
annotation mentions that the following must be true:
1. The result ofI assume this means that two instances of a class with the same internal values should evaluate towill always return the same result for the same two instances.equals
true
using equals
. However, this is not the case for ColorScheme
. Am I misunderstanding what this means?MR3Y
08/23/2022, 5:26 PMI assume this means that two instances of a class with the same internal values should evaluate toNo, this is not what the documentation ofusingtrue
.equals
@Stable
is meaning. what it actually means is that calling equals
on the same two instances should always return the same result even if that result was false
, that doesn't violate stable contract.
Also, see: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1660409798793859?thread_ts=1660409095.230819&cid=CJLTWPH7SMR3Y
08/23/2022, 5:28 PMColorScheme
isn't overriding equals
, so, it will fallback to the default equals
that is always returning false
. That being said, ColorScheme
complies with @Stable
contractAndrew Hughes
08/23/2022, 5:39 PMAndrew Hughes
08/23/2022, 7:48 PM@Stable
? The documentation states:
When applied to a function or a property, theIf I have a function that returns an instance of a class that is markedannotation indicates that the function will return the same result if the same parameters are passed in. This is only meaningful if the parameters and results are themselvesStable
,Stable
, or primitive.Immutable
@Stable
, does that function need to return the exact same instance if the same parameters are passed in? Or can it return a different instance that is functionally equivalent?MR3Y
08/23/2022, 8:13 PM@Stable
contract, then the function can be stable.Andrew Hughes
08/23/2022, 8:21 PM@Stable
instance A
as a parameter to a function and it returns an @Stable
instance B
. Does annotating the function with @Stable
require that the instance B
be returned every time A
is passed in? Or could it return a different instance (C
, D
, ...) each time it is called with A
, as long as the the returned value is "the same" (not necessarily equal
or the same instance)?Andrew Hughes
08/23/2022, 8:25 PMA
is mutated (while complying with the @Stable
contract)? In my scenario, this could change the output of the function. The question is, how does that affect the stability of the function?MR3Y
08/23/2022, 8:44 PMIf I pass in aninstance@Stable
as a parameter to a function and it returns anA
instance@Stable
. Does annotating the function withB
require that the instance@Stable
be returned every timeB
is passed in? Or could it return a different instance (A
,C
, ...) each time it is called withD
, as long as the the returned value is "the same" (not necessarilyA
or the same instance)?equal
it doesn't have to be the same instance, just make sure that calling
B.equals(C or D...)
will always return the same result whether it is false
or true
, this is what @Stable
cares about , if B
& C
or D
have the same values but for some reason or another their equals
call isn't deterministc, then they shouldn't be marked as @Stable
because compose compiler can recompose those incorrectly based on your assumption.Andrew Hughes
08/23/2022, 8:48 PMequals
? It sounds like it's referring to the result of the function.
If I'm understanding you correctly, that would mean any function that returns an @Stable
value can be marked as @Stable
. But I don't think that's the case. Maybe I'm misunderstanding.MR3Y
08/23/2022, 8:49 PMAnd what ifis mutated (while complying with theA
contract)? In my scenario, this could change the output of the function. The question is, how does that affect the stability of the function?@Stable
It is normal, The compose compiler would recompose that to read the updated value
MR3Y
08/23/2022, 8:51 PM@Stable
annotation is to aid the compiler in making smart optimizations for you for things that aren't optimized by defaultMR3Y
08/23/2022, 8:56 PMMR3Y
08/23/2022, 9:00 PMIf I'm understanding you correctly, that would mean any function that returns anvalue can be marked as@Stable
. But I don't think that's the case. Maybe I'm misunderstanding.@Stable
You're right, any function that takes stable & returns stable can be marked as stable
Andrew Hughes
08/23/2022, 9:08 PM@Stable
on a bunch of things. I just thought it might make sense for this once scenario, but I'm still not wrapping my head around it for functions.
Here's what I'm doing:
I've created a TonalPalette
class that's basically a copy of the implementation of ColorScheme
, but with all the base tonal values for each tonal range. This is annotated with @Stable
, just like ColorScheme
. Then I have a fun TonalPalette.lightColorScheme(): ColorScheme
that creates a ColorScheme
from the appropriate values from the TonalPalette
(as well as a corresponding TonalPalette.darkColorScheme()
function). Initially, it made sense to me that these functions could be annotated with @Stable
as well, but now I'm not so sure. Does that make sense?
The functions are used in our AppTheme
composable, which wraps MaterialTheme
, and determine the appropriate ColorScheme
based on the current TonalPalette
and darkTheme
parameter. I assume it'd be helpful if the functions generating the ColorScheme
only were called and/or created new ColorScheme
whenever TonalPalette
or darkTheme
changes. Does that sound like an appropriate use of @Stable
on these functions? Or should I just not worry about it?Andrew Hughes
08/23/2022, 9:10 PMYou're right, any function that takes stable & returns stable can be marked as stableOkay, then if a function takes a stable and returns a stable, does the compiler already implicitly mark it as stable? Or is it still necessary to manually annotate it?
MR3Y
08/23/2022, 11:06 PMI've created aYes, it perfectly makes senseclass that's basically a copy of the implementation ofTonalPalette
, but with all the base tonal values for each tonal range. This is annotated withColorScheme
, just like@Stable
. Then I have aColorScheme
that creates afun TonalPalette.lightColorScheme(): ColorScheme
from the appropriate values from theColorScheme
(as well as a correspondingTonalPalette
function). Initially, it made sense to me that these functions could be annotated withTonalPalette.darkColorScheme()
as well, but now I'm not so sure. Does that make sense?@Stable
MR3Y
08/23/2022, 11:09 PMOkay, then if a function takes a stable and returns a stable, does the compiler already implicitly mark it as stable? Or is it still necessary to manually annotate it?honestly, I'm not sure if the compiler would implicitly mark it or not, I would need to check the underlying code for that. But, I think the most common approach is to annotate that manually since there are many functions in compose UI, material, and foundation are already doing that
MR3Y
08/23/2022, 11:13 PMMR3Y
08/23/2022, 11:15 PM@Stable
Grégory Lureau
08/24/2022, 8:01 AMYou're right, any function that takes stable & returns stable can be marked as stableWhat if there is side effects (or even local random)? I think about a function using the system time or a static variable/function where the result can change depending of the timing of the call, and it impacts the output (let's say a stable data class that contain the result). If I get the goal of @Stable right, it should not be marked as stable because we want the compiler to call it again even if the parameters haven't changed. Am I right?
MR3Y
08/24/2022, 11:44 AM@Stable
.
On a side note, I wanted to ask you one question, do you have a usecase for when a function that should be stable depends on some global variables? because I can't think of a usecase for it or haven't seen one like that yet. Or is it just a contrived example to better understand @Stable
?Grégory Lureau
08/24/2022, 11:56 AMLocalContext.current
because it's a @Composable method.MR3Y
08/24/2022, 12:08 PMcomposed
and allows you to access composable functions/getters inside factory
lambda body. For example: Modifier.clickable is a stateful modifier. if you're talking about those, as far as I've seen, no, those aren't marked as @Stable
MR3Y
08/24/2022, 12:09 PM@Stable