What is the recommended doctrine for using the AutoOpen attribute ?
(This question is probably is in continuation to when to use module function VS static function on similarly named type )
Expert F# states that “This can be useful when you’re defining ad hoc top-level operators
and functions:”
So this seems to be here to kind of diminish the module role in the code organization, when you technically need one to write the code but you remove its existence from a client perspective.
Is there something else ?
When do you use it ?
I think the main use for the
AutoOpenattribute is when you want to make some let-bound values available when the user of your library opens a namespace. This is where the attribute is very useful, because I think libraries should generally export all definitions in namespaces, but for some purposes you need to export values and values cannot be defined inside a namespace.Here is an example from F# async extensions which defines a computation builder and thus it needs to export
asyncSeqvalue (but at the same time, all functionality is wrapped in a namespace):The user of the library can just write
open FSharp.Asyncand they will seeasyncSeq. I think the same pattern is used with various math libraries (where you also want to export simple-named functions.)For modules (e.g.
ListandSeq), I think most of the people do not useopenand access the functions via a module name (such asList.map), so although you can use this for nested modules, I have not seen that as frequently.