Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsafePerformEffect #2

Open
jamesdbrock opened this issue Oct 6, 2020 · 1 comment
Open

unsafePerformEffect #2

jamesdbrock opened this issue Oct 6, 2020 · 1 comment

Comments

@jamesdbrock
Copy link
Member

Is it a good idea to run execPut in unsafePerformEffect? I suspect that it is a very good idea.

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Oct 6, 2020

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 2

serializer :: 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

Caveat emptor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant