package bin

  1. Overview
  2. Docs

Module Bin.StagedSource

For performance reasons, it is sometimes preferable to handle functions specialising in one or two arguments rather than applying all the function’s arguments (and repeating internal calculations whenever that first or second argument appears). The Staged module allows you to define a boundary between what should be ‘pre-applied’ and what should be fully applied afterwards.

let fn x =
  let x = x + 1 in
  Staged.stage (fun y -> x + y)

let g = fn 20
let () = print_int ((Staged.unstage g) 21)

In the example above, we prepare the function g so that the calculation x+1 is already done and no longer needs to be performed. Furthermore, the application of g will only involve the calculation x+y.

In the case of Bin, it may be useful to prepare how a format is to be decoded or encoded. Furthermore, decode and encode_bstr return a Staged.t value in order to force these functions to specialise via the description of a binary format.

In this way, the decoder/encoder for this format is properly specialised and optimised. This is the main reason for the existence of this module.

Sourcetype +'a t
Sourceval stage : 'a -> 'a t
Sourceval unstage : 'a t -> 'a