Let assignment with local open?

Hello. I am new to OCaml and was researching OCaml SQL libraries and stumbled upon GitHub - Gopiandcode/petrol: Petrol's an OCaml SQL API made to go FAST..

There is an example in the README of that project:

(* declare a table *)
let example_table, Expr.[name; age] =
    StaticSchema.declare_table schema ~name:"example"
    Schema.[
        field "name" ~ty:Type.text;
        field "age" ~ty:Type.int
    ]

I don’t understand what is going on with Expr.[name; age]. At first I thought this was a local open. But it appears that name and age are being introduced, i.e. newly assigned to, so I don’t understand what the Expr. local open would accomplish. As far as I can tell, neither name nor age are existing types or values inside the Expr module.

(I cloned the project and tried to run the tests, but I couldn’t figure out how to. dune test doesn’t seem to do anything).

Would someone be able to help me understand this OCaml syntax? Thanks.

1 Like

Hello, you’re absolutely correct - it is a local open. It is introducing these constructors for the custom list syntax. If you define the constructors :: and [], you can use the list syntax sugar [a;b;] to access them instead of a::b::[]. To be able to access those constructors in the pattern part of the let binding, the local open was necessary here.

4 Likes