# This type cannot be unboxed

**URL:** <https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367>\
**Category:** Learning\
**Created:** [October 1, 2024, 3:10pm UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367 "2024-10-01T15:10:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 1, 2024, 3:10pm UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367/1 "2024-10-01T15:10:36Z")

</div>

but I would like it to be (ocaml v5.2.0). Is there any workaround ?

```ocaml
module A : sig
  type 'a t
  type exist = V : 'a t -> exist [@@unboxed]
end = struct
  type 'a t = { value : 'a; }
  type exist = V : 'a t -> exist [@@unboxed]
end

```

```sh
[FAIL] [exec-build ocamlopt][2]: (brzo log --id 10)
File "/Users/dbuenzli/tmp/test/a.ml", line 4, characters 2-44:
4 | type exist = V : 'a t -> exist [@@unboxed]
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable "'a".
       You should annotate it with "[@@ocaml.boxed]".

```

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [October 1, 2024, 3:55pm UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367/2 "2024-10-01T15:55:56Z")

</div>

One way is by disabling the flat-float-array “hack”.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [October 1, 2024, 3:59pm UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367/3 "2024-10-01T15:59:36Z")

</div>

I suspect you’re already aware of those alternatives, but just in case:

- Using a compiler configured with `-no-flat-float-array` will work
- Exposing the type as `type 'a t = private { value : 'a }` will work

Apart from that, I have seen proposals for a `[@non_float]` attribute that would solve your problem properly, but I think they all failed to get through. (I found [#7485](https://github.com/ocaml/ocaml/issues/7485), [#1261](https://github.com/ocaml/ocaml/pull/1261), and I think someone proposed it again on the Jane Street compiler but I can’t find it.)

Looking at the type-checker code more in detail, your example would work if you could put an annotation on the `'a` parameter of `type 'a t` that tells the type-checker that `'a t` is separable (either always float or always non-float) independently of `'a`. The type-checker actually tracks this property internally, so all that’s missing is syntax (like the variance and injectivity annotations) to let programmers write the annotation.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 1, 2024, 4:10pm UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367/4 "2024-10-01T16:10:19Z")

</div>

Thanks for your answers. None of that looks very enticing :–) Perhaps I’ll simply move back to boxed existentials with preallocated ones in `'a t` via `let rec`.

---

<div class="post-metadata">

**Author:** ![amongonz](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@amongonz](https://discuss.ocaml.org/u/amongonz)\
**Post date:** [October 3, 2024, 9:22am UTC](https://discuss.ocaml.org/t/this-type-cannot-be-unboxed/15367/5 "2024-10-03T09:22:05Z")

</div>

If the float hack isn’t going away anytime soon, maybe we need an `[@@unboxed_when_non_float]` attribute instead.
