Reading from buffers should require Eff
Closed this issue · 4 comments
Consider the following program:
module Main where
import Prelude
import qualified Node.Buffer as B
import Control.Monad.Eff.Console (log)
main = do
let buf = B.fromArray [0]
let x = B.read B.UInt8 0 buf
B.write B.UInt8 1 0 buf
let y = B.read B.UInt8 0 buf
log ("x = " ++ show x)
log ("y = " ++ show y)
Output:
x = 0
y = 1
This seems to be a subversion of the type system. I think we should require Eff to read or write from a buffer. Since they can be used in a similar way to IORef in Haskell, perhaps we also should require Eff to create them, like Haskell does.
(Unfortunately this program doesn't actually run at the moment but that problem is fixed by #4)
Agreed!
And creating them too, I think.
module Main where
import Prelude
import qualified Node.Buffer as B
import Control.Monad.Eff.Console (log)
main = do
let buf = B.fromArray [0]
B.write B.UInt8 1 0 buf
let r = log <<< show <<< B.read B.UInt8 0
r buf
r (B.fromArray [0])
prints
1
0
Here, since we've defined buf = B.fromArray [0], it should therefore be possible to replace buf with B.fromArray [0] and not change the meaning. So I think we should have fromArray :: forall eff. Array Octet -> Eff (buffer :: BUFFER | eff) Buffer, and similar for create.
Yep, probably everything should have the effect because they are inherently mutable things. I think I'm responsible for the crimes in here. 😉