For statement type instability

You don’t need to do anything. In this case Julia will just unroll the loop later down the compilation pipeline and you won’t notice anything from this type-instability. In any case, the number of types is also small enough for union-splitting (where the magic number is 4 IIRC).

As a general rule of thumb: Small unions of concrete types are likely fine. You don’t need to fix anything. I’d say fixing “type instabilities” just for the sake of it is rather pointless. If you want to do it for performance optimization, you need to demonstrate that this is a performance problem to begin with.