Search code examples
ocamlnaming-conventionsconventions

What's the OCaml naming convention for "constructors"?


An OCaml module usually contains at least one abstract type whose idiomatic name is t. Also, there's usually a function that constructs a value of that type.

What is the usual / idiomatic name for this?

The StdLib is not consistent here. For example:

  • There's Array.make and a deprecated function Array.create. So that function should be named make?
  • On the other hand, there's Buffer.create but not Buffer.make. So that function should be named create?

Solution

  • I had the same question when I picked up the language a long time ago. I never use make and I think few people do.

    Nowadays I use create for heavy, often imperative or stateful values, e.g. a Unicode text segmenter. And I use v for, functional, lighter values in DSL/combinator based settings, e.g. the various constructors in Gg, for example for 2D vectors, or colors.

    As camlspotter mentions in his answer the standard library distinguishes make and create for values that need an initial value to fill in. I think it's better to be regular here and always use create regardless. If your values support an optional initial fill value, add an optional argument to create rather than multiply the API entry points.