You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear computer programmer, you should definitely call execPut inside unsafePerformEffect (if you want to).
The reason why this whole library is Effectful is that ArrayBuffers are mutable, so by default, the ArrayBuffer library functions do everything in Effect to enlist the compiler to confirm that any mutations are properly sequenced.
However, all of the put functions in this library are actually side-effect free, so we can use unsafePerformEffect to tell the compiler to trust that we don't perform any side-effects while we're building an ArrayBuffer.
let pureArrayBuffer ::ArrayBuffer
pureArrayBuffer = serializer 2serializer::Int->ArrayBuffer
serializer x = unsafePerformEffect $ execPut $ do
putInt32be x -- ✅ Good, this has no side-effects
putInt32be =<< getCurrentPosixTime -- ❌ NO NO DON'T DO THIS INSIDE unsafePerformEffect
putArrayBuffer =<< readFileFromMyFilesystem -- ❌ NO WE ARE GONNA GET SO BURNED
Activity
jamesdbrock commentedon Oct 6, 2020
Dear computer programmer, you should definitely call
execPutinsideunsafePerformEffect(if you want to).The reason why this whole library is
Effectful is thatArrayBuffers are mutable, so by default, theArrayBufferlibrary functions do everything inEffectto enlist the compiler to confirm that any mutations are properly sequenced.However, all of the
putfunctions in this library are actually side-effect free, so we can useunsafePerformEffectto tell the compiler to trust that we don't perform any side-effects while we're building anArrayBuffer.Caveat emptor.