How to cast foreign types?

How do you cast a foreign types like CInt to Int, or cast CInt to CLong, and moreover, how to deal with Enum. So far, for all of them I’ve been combining toEnum and fromEnum, but this seems hacky. Is there a proper way to do this?

I think fromIntegral should work here in both cases.
For Enum types it might depend on the concrete type but using fromEnum/toEnum seems fine.

2 Likes

Thank, that does work.

They’re ordinary newtypes from Foreign.C.Types:

import Foreign.C.Types qualified as FFI

to :: (Int32, Int64, Int) -> (FFI.CInt, FFI.CLong, FFI.CBool)
to (a, b, c) = (FFI.CInt a, FFI.CLong, toEnum c)

fro :: (FFI.CInt, FFI.CLong, FFI.CBool) -> (Int32, Int64, Int)
fro (FFI.CInt a, FFI.CLong b, c) = (a, b, c)