I’m trying to create a first-class module an am finding it cumbersome to specify the type. First, it is kind of annoying that I can’t just wrap up the module without specifying a type at all. And then when I try to specify the type inline
- see below - I get a syntax error. Why can’t the module type be specified inline
? I must create a whole new module type X
that is then used in the statement that creates the first-class module. Is there a less cumbersome syntax for doing this?
module type Validated = {
type t
type domain
let make: domain => option<t>
let makeExn: domain => t
let value: t => domain
}
module type Config = {
type domain
let validate: domain => option<domain>
}
module type MakeValidated = (P: Config) => (Validated with type domain := P.domain)
module Make: MakeValidated = (P: Config) => {
type t = P.domain
let make = v => v->P.validate
let makeExn = v => v->make->Option.getExn
external value: t => P.domain = "%identity"
}
module Number1To100 = Make({
type domain = int
let validate = n =>
switch n >= 1 && n <= 100 {
| true => Some(n)
| false => None
}
})
// Works but annoying that I have to create a named type just for
// this one line, and also that I have to repeat the domain := int
// that was used to construct the module
module type Number1To100Type = Validated with type domain := int
let num1To100_A: module(Number1To100Type) = module(Number1To100)
// Syntax error specifying the type inline
let num1To100_B: module(Validated with type domain := int) = module(Number1To100)
// The signature for this packaged module could not be inferred
let num1To100_C = module(Number1To100)