8754Re: "ocaml_beginners":: Should I 'open List'?
- Oct 4, 2007On Wed, 3 Oct 2007, Michael Feathers wrote:
> I understand that it's poor practice to open modules, and that we'reI used to open List, but no longer do this. I appreciate the brevity, but
> 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 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 wouldI open Printf too. Printf.sprintf is just too redundant, and I use it far
> 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.
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
let v = unqualified_f x
This can be particularly handy for record types.
- << Previous post in topic