Has anyone looked at teaching arrow analysis how t...
# arrow
t
Has anyone looked at teaching arrow analysis how to keep track of composition local usages in function body’s to imply the required composition locals. Then i’d imagine the call site of a function could be validated? I’ve not ever used the tool but it seems like the sort of thing it could do after listening to the kotlin talks conversation. For those not working in android land: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal You can sort of think of comp locals as an implicitly available map to pull things from.
c
I'm not part of the Arrow team, but IMO if you need to validate that a CompositionLocal has been passed, you're using them incorrectly.
What you probably want is to pass your data as receiver.
t
You aren’t wrong but even that sort of goes out the window with testing
c
I kinda wish context receivers were here already, because they will bring a proper solution to many cases where CompositionLocal is the current workaround
t
I had the same thought. Comp Locals end up being an implicit and dynamic build up of a context receiver
c
Maybe you could use
providesDefault
in your component? This way if the caller forgot to provide one, there is still one
t
I tend not to do that. I really like to be very strict about them. no null, no default. you configure it right or it blows up
c
Makes sense. I don't think you can do better than blowing up at runtime though.
t
ok fair enough.
c
My understanding is that CompositionLocal was designed on purpose so that the caller cannot know if one is necessary or not, so they don't include implicit dependencies throughout the call chain
t
makes sense, this really only comes up in some weird biz cases for us. Literally biz logic to modify theme based on joint venture partners that sponsor a user account. sort of ties things that usually are no big deal to comp local to things you shouldn’t
then the obvious analytics interfaces where composed modifiers are just great for building easy to use api’s around user interactions and time spend tracking
c
CompLocal for themeing sounds fine
t
we did some adjustments recently to make it so you can forget in your test that uses
setContent
so it will default to our default theme if you forget to overload the JV theme lookup. but I don’t love that either because it leaves a gap where if you mess up on which base class to use you can end up in a situation where it would fail to the default theme rather than blow up. trying to balance the ergonomics of it all with correctness really. the idea of arrow being able to explode in a CI build with missing CompLocal’s seemed really cool. Plus having maybe eventual IDE tooling where you could do like the CMD+P and it would also list what CompLocals are used internally would be probably useful.
c
About the IDE support, you can already do that if your CompLocal is defined in an attribute
But I think that's the only way to use them since you have to refer to them to access their value?
t
Yeah the typical way to use them is a wrapper object
Copy code
object LocalAnalytics {

    private val local = staticCompositionLocalOf<Analytics?> { null }
    val current: Analytics
        @Composable
        @ReadOnlyComposable
        get() {
            val current = local.current
            return checkNotNull(current) { "useful message omitted" }
        }

    infix fun provides(registry: Analytics): ProvidedValue<Analytics?> = local.provides(registry)
}
then elsewhere you just do a
LocalAnalytics.current
which in this case is all inside of composed modifiers.
CompositionLocal<T>
has a
val current: T
so i’d expect it would look for reads of that
current: T
to build the list of what is used. if
T
is nullable maybe treat it as optional. then analyze the call site to see what
ProvidableCompositionLocal<T>.provides[Default]
usages have been used in your call stack. warning for missing
T?
locals and errors missing
T
’s