Linter for unboxing OCaml types
There are few reasons not to unbox types that can be. So let's find them!
Unboxing is allowed for:
- records with a single field:
type t = { f : u }
- variants with a single constructor with a single parameter:
type t = C of u
type t = C of { f : u }
- allowed for parametric types, GADTs
- disallowed if the field is mutable
Pros:
- smaller memory footprint
- faster accesses
- no type safety concession
Cons: usage of magic features
- physical equality
type t = C of string
let s = "blah"
let x = C s
let y = C s
x == y (* true if [t] is unboxed, false otherwise *)
- non-regression on
- marshalling
- hashing
- cyclic values
type t = C of t
let rec x = C x (* forbidden if [t] is unboxed, allowed otherwise *)
For cases where we want to avoid unboxing, let's have a special annotation [@@do_not_unbox "reason"]
.
Edited by Mehdi Bouaziz