Open dcoutts opened 2 hours ago
Related: it'd also be nice to have specific Unbox
instances for types like ByteArray
and MutableByteArray
that are (morally) unlifed but boxed. This could take advantage of recent GHC's ability to have levity-polymorphic arrays (which ideally should be exposed via Primitive.Array
).
And further refinements: we could also have BoxedStrict
for cases where we want a vector of boxed data, but want the values in the array to be always evaluated to WHNF. Currently it's actually quite hard to reliably have vectors of boxed but WHNF data.
Yes. I would say that Unbox is misnomer. It about selecting representation of an array by element type. And it need not to be necessarily unboxed.
There's also precedent UnboxViaPrim
uses primitive vector as a representation. It could be used as a template. I would gladly accept PR with such addition. Only question is naming. I think it would be nice to be explicit about strictness of underlying array.
I can see two possible names: AsLazyBoxed/AsStrictBoxed
using As
as reference. Or UnboxVia{Strict,Lazy}Boxed
which is based on UnboxViaPrim
but it's long and weird. So former it probably better
It may sound counter-intuitive, but it would be really useful for unboxing structures that contain some boxed data.
For example, the docs for
As
give this little example record data type:It defines a relation between this and a tuple (for which there is an existing
Unbox
instance)and end up defining an instance
But this requires the
a
to be unboxable too. What if I have a record member that really does need to be boxed, such as a reference, another array, a string, etc etc etc.I should instead be able to define the relation like this
where
Boxed
is a provided newtype that is an instance ofUnbox
already, and thus the tuple(Int, Boxed a)
is therefore an instance ofUnbox
.Obviously, the representation for a vector of
Boxed
is just an ordinary boxed vector!And that's it. Now we can unbox any record, even if some fields are still boxes.
The above code type checks, it's just missing the impl for all the methods, but that's just doing the obvious boring thing. A PR would be easy.