In the Core
ecosystem (and Real World OCaml as a result), there seems to be a trend towards putting types and their strongly associated functions (accessors, constructors, etc) into a singe module, so instead of
type foo = { bar: int; baz: bool };;
let baz_of_foo { baz; _ } = baz;;
we’d generally go for
module Foo = struct
type t = { bar: int; baz: bool };;
let baz { baz; _ } = baz;;
end
(excuse my bad syntax!)
My wondering is where one draws the line between situations where putting a type in its own module (and naming it t
, and associated conventions) is a good idea, and where it makes more sense to leave it as a single, directly named type. To me it feels like anything that is a natural abstract data type (and thus should have information hiding, etc) is an obvious candidate for a module, but beyond that it’s not entirely obvious.
Quite often I’ve ended up starting with named types but moved them to modules anyway, as the centralisation of all of the type’s strongly associated functions and bits of functionality—as well as being able to include
common signatures—becomes a big win.
Are there in fact any real disadvantages to modularisation, except for verbosity? One thing I worry about is tripping over non-OCaml or non-Core
programmers who don’t necessarily know that Foo.t
should be read as ‘foo
, but with a module surrounding it’. Similar issues with Foo.S
for 'signature of things that are a Foo
'.
(I realise this is a very subjective topic, but I was wondering what people’s intuition is on when to make the leap.)