Somebody claiming to be Simon Peyton-Jones wrote:
| It so happens that on 64 bit OS X, haskell's Int is 64 bits, while C's
| int is
| 32 bits.  hsc2hs's #poke does no typechecking at all, so if you have
|
| (#poke Struct, int_field) structp int -- where int :: Int

One should not do this.

That said, the tool should not allow this.

| It's probably not even correct to
| assume Double = CDouble and Float = CFloat, though it seems likely to be
| always true.

base has a configure check for these, but I have been unable to determine what it does. It fails on my QNXNTO cross-compiler and so I've been manually hacking this assumption in there, but ... yeah ... probably not safe.

| I carefully vetted all my #pokes but I'm annoyed that hsc2hs and Foreign
| let me get away with this.  If Storable is for marshalling types to and
| from C, then non-C types should not be Storable!

Well, except that you might just be round-tripping some Haskell data through (void*) in C, and so the interpretation on the C side does not matter in that case.

| While we're at it, fromIntegral is not a safe way to convert a haskell
| type to a C one.  But it sure is a tempting one

It is the one I always use, but I'm very aware that the behaviour is likely to be bad. That said, the behaviour of `fromIntegral` moving to a smaller type is always bad. This should probably exist:

fromIntegralSafe :: (Integral a, Ord a, Bounded b, Integral b) => a -> Maybe b
fromIntegralSafe x
        | toInteger x > toInteger (maxBound `asTypeOf` v) = Nothing
        | toInteger x < toInteger (minBound `asTypeOf` v) = Nothing
        | otherwise = Just v
        where
        v = fromIntegral x

| Or maybe I shouldn't be using hsc2hs in the first place.

Hard to say. I've used it a couple times, mostly when there are C enums involved. Usually in my FFI work I just write the imports by hand (though this also allows for dangerously writing `Int` when you mean `CInt`, etc).

--
Stephen Paul Weber, @singpolyma
See <http://singpolyma.net> for how I prefer to be contacted
edition right joseph

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Cvs-ghc mailing list
Cvs-ghc@haskell.org
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to