Loading ...
Sorry, an error occurred while loading the content.

12083Explicit parameter names in .mli files

Expand Messages
  • iainmcgin
    Aug 5, 2010
    • 0 Attachment
      Hello all,

      I am new to OCaml, and not yet familiar with its idioms and "good style", so there will likely be many criticisms along those lines regarding what I am about to ask. Please do make these criticisms, as I'm eager to learn good style and idiomatic OCaml as much as what the language actually allows in the general case.

      One big problem I've had with trying to read existing OCaml module interfaces is the absence of parameter names in module interfaces, and the absence of type information in the module implementation. I have to look in two places simultaneously to see both, of course assuming that the function in question is explicitly documented in the interface. I understand the benefits of type inference in removing the need to specify type information everywhere, but it still feels important to me that for top level functions, if only for documentation purposes, that parameters be named (alluding to their intended usage), and their types supplied (giving supplemental information to aid understanding of what the function does).

      So, given my desire to do this (which may be misguided or not idiomatic, and I should just learn to read code without type annotations), I discovered that labels can be provided for parameters on functions in the .mli files, like so:

      val f : x:string -> y:string -> string

      The "problem" this introduces for me is that the parameters in the implementation must also be labelled. This does not type check:

      let f x y = x ^ y

      While this does:

      let f ~x ~y = x ^ y

      What I have been doing, so that I can have the type information in both the .mli and the .ml files, is:

      let f ~(x:string) ~(y:string) = x ^ y

      Which is somewhat "ugly", but it allows me to say what I want. One consequence of defining functions in this way however is that partial application is less straightforward. The following does not typecheck:

      let a = f "string"

      With the following error:

      > Error: The function applied to this argument has type
      > x:string -> y:string -> string
      > This argument cannot be applied without label

      Instead I must do:

      let a = f ~x:"string"

      A related issue occurs if I accidentally specify too many parameters for function f:

      let b = f "string" "string2" "string3"

      This produces the same error as above, and it is not immediately obvious whether this is because I have over or underspecified the number of parameters.

      What I would really like is named parameters, but only for documentation purposes, in .mli files, without these names being treated as labels, which seem to be the main cause of my problems above. Is this possible? If not, through what other means could I document what the intent of each parameter is (i.e. ocamldoc annotations, etc)?

      Thanks for listening,
      Iain McGinniss
    • Show all 5 messages in this topic