When writing FFI bindings, unsafeCoerce
is quite ok: after all, foreign import
has all the same drawbacks already, so you're not really losing anything.
And yes, what you came up with - R1orR2
- is the right approach that's used quite frequently in FFI bindings.
You may also want to check out undefined-is-not-a-problem
and untagged-union
libraries. They offer some more advanced and generalized techniques in this area.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…