Let's say I have several independent animations wh...
# compose
r
Let's say I have several independent animations where I want to use the same AnimationSpec for, e.g.
tween(durationMillis = 100)
Let's also say they are animating different types, e.g.
animateFloatAsState
,
animateColorAsState
etc.. What's the best way to share a common animationSpec in this situation? I can't just assign the same spec, because the AnimationSpec is generic, e.g.
AnimationSpec<Float>
.
z
i think you could just cast the generics? I don't think any of them actually have type-specific implementations
💯 1
if you want to be less hacky, you could also just make your own factories:
Copy code
fun <T> myAnimationSpec() = tween(durationMillis = 100)
r
Oh interesting, didn't realise they weren't type-specific. I assumed they were because they were generics! A factory looks like it'll work well, although passing the animationSpec in to the composable as a parameter then presents a challenge (I can't think of an ergonomic way to do it without a non-fun interface)
z
if a function doesn't take a reified generic, then it can't change behavior based on the type because it has no idea at neither compile-time nor runtime what the concrete type is.
t
It can certainly choose to crash at runtime, though :)
Mostly via
ClassCastException
z
Non-generic code can do bad casts too ¯\_(ツ)_/¯