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

8754Re: "ocaml_beginners"::[] Should I 'open List'?

Expand Messages
  • Dave Benjamin
    Oct 4, 2007
      On Wed, 3 Oct 2007, Michael Feathers wrote:

      > I understand that it's poor practice to open modules, and that we're
      > better off using explicit qualification, but I was wondering whether
      > there are any disadvantages to opening List in a module? Do experienced
      > Ocamlers do this?

      I used to open List, but no longer do this. I appreciate the brevity, but
      I don't like the asymetry between "iter" and "Array.iter", for example.
      Many modules expect that you will qualify them, and this is reflected in
      the names of functions they export, such as "Array.of_list". It'd be
      pretty odd to use "of_list" by itself, no? However, unlike many standard
      modules, List's methods seem to all make sense on their own, so perhaps it
      was intended that some people might open it.

      When writing code, I like the brevity of unqualified names. However, when
      reading code (other people's code, especially), qualified names are nice
      because I don't have to do so much work to figure out where a name comes
      from. So, out of consideration, I tend to avoid "open" as much as I can.

      One benefit of "open", though, is that it can serve to document the
      dependencies of a module, somewhat like the "import" statements in other
      languages. However, the modules really have to be designed with this in
      mind, and many aren't.

      > I do open Printf and it seems that there's nothing there that would
      > clash with anything. Some of the names in List are things I can imagine
      > clashing.. names like 'length', but on the other hand, it feels weird to
      > use explicit qualification for something as elemental as lists in a
      > functional language.

      I open Printf too. Printf.sprintf is just too redundant, and I use it far
      too often to qualify it every time. However, this is just about the only
      module I open.

      > What is accepted practice on this?

      I can't speak for everyone, but in general your understanding is correct;
      it's considered bad for to open modules, except for those (rare) cases
      where the module is designed to be opened.

      It would be nice if something like pa_openin was added to the standard
      syntax. I find it hard to justify a syntax extension for something so
      trivial, though. Occasionally, I simulate a local open with a "let module"

      let module M = struct
      open AModule
      let v = unqualified_f x
      end in

      This can be particularly handy for record types.

    • Show all 18 messages in this topic