Skip to content

unsafePerformEffect #2

Open
Open

Description

@jamesdbrock

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

Activity

  1. jamesdbrock commented on Oct 6, 2020

    @jamesdbrock
    MemberAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions