Is anyone using dependency injection / oop for the...
# compose
s
Is anyone using dependency injection / oop for theming in compose? I see a lot of top level declaration in samples / material documentation
c
I think themes end up being one of those cases where Composition Locals (better known as Globals IMO) make sense to use instead of having to pass them down absolutely everywhere.
r
Well they are local to a composition. They're not true global values because of that. They're scoped globals if you prefer ^^
j
With class loader hierarchies technically globals are also scoped globals
c
I still don't really understand the name composition locals. Isn't any variable defined in a composable a composition local?
a
I think they were called ambients before. At least there's a few references to them in the pathway docs. The name is confusing, yeah. I guess you can think of those values as local to a composition tree. I just think of them as scoped globals.
I also use them with default function parameters as a cheap alternative to dependency injection.
j
@Colton Idle a variable defined inside a
@Composable
is more like a recomposition local as its lifetime is tied to a single recomposition. Composition locals survive for the lifetime of the composition object across multiple recompositions
c
Oooh. That's actually super helpful. Recomposition local vs composition local.
1
j
Well... kinda. It's mostly that. You probably won't harm yourself with that mental model, but there's also more nuance since they're hierarchical and can exist at different points in the tree.
Technically the composition owns the tree and therefore holds these composition locals, but since they can be installed at various points of the composable hierarchy installing a composition local does not mean it's visible to 100% of the composables in that composition
👍 1
A name which is fully accurate would probably be like 100 characters long and "composition local" is Close Enough™
👆 1
r
We moved away from
Ambient
because we deemed that term even more confusing
c
Should have just gone with
Context
to really mess with all of us.
a
"You only had a fuzzy idea what the context was before. But good news! You can now define your own!"
z
CompositionLocal is also meant to pattern match to ThreadLocal. Just like TLs, CLs can have different values depending on where they're read.
👍 1
👆 2
c
Time to lookup what a ThreadLocal is. 😅
1