Recently I saw Consider making distinct types for pinned and unpinned arrays · Issue #405 · haskell/primitive · GitHub, which suggests adding a type parameter to ByteArray
and PrimArray
to indicate the “pinnedness” of the array, i.e. wether it is pinned or unpinned (pinned arrays can not be moved by the garbage collector). This would look something like this:
data Pinnedness = Pinned | Unpinned
data ByteArray (p :: Pinnedness) = ...
data MutableByteArray (p :: Pinnedness) s = ...
newByteArray :: PrimMonad m => Int -> m (MutableByteArray 'Unpinned (PrimState m))
newPinnedByteArray :: PrimMonad m => Int -> m (MutableByteArray 'Pinned (PrimState m))
While this would be a breaking change and thus likely won’t be implemented in primitive
, it piqued my interest. Since some operations are only safe on pinned arrays (e.g. byteArrayContents
), this could be used to statically enforce that they’re only used on pinned arrays. However, it would make it harder to mix pinned and unpinned arrays.
I’d like to know if anyone here would find this useful and if you’d use a package for this, that provides thin wrappers over ByteArray
and PrimArray
with a “pinnedness” type parameter.