Use Number instead of Int for `read`
matthewleon opened this issue · 3 comments
I've made a branch here to show this in a test: https://github.com/matthewleon/purescript-node-buffer/tree/uint32-test
Relevant code:
testUInt32 :: Test
testUInt32 = do
buf <- fromArray [-1, -1, -1, -1]
readVal <- read UInt32LE 0 buf
logShow readVal -- 4294967295
logShow $ readVal - 1 -- -2
-- chaos reigns: try to uncomment the line below
--logShow $ readVal == 4294967295
Purescript Ints really don't play nice with these operations. They probably require something like https://github.com/zaquest/purescript-uint
Yes, we should have these functions use a UInt type. I'd rather it be in contrib if we're going to depend on it in this library, though.
Makes sense. I'd be happy to see it make its way into contrib.
Come to think of it, we really ought to be using Number as the return type of read, since you can read a Double{BE,LE} or a Float{BE,LE} out of a Buffer, and clearly any integer type is unsuitable for that. Using Number as the return type addresses the UInt32 issue too. Since these libraries are meant to be low-level bindings, I think the loss of convenience is acceptable.