Opening a namespace in F#.net for a specific block of code

.net, f#, namespaces

Solution

The smallest scope for opening namespaces is in a module.

A workaround is to put a function and its opening namespaces into an auto-open submodule in order that `open` commands do not pollute parent modules and the submodule is transparent to users:

[<AutoOpen>]
module Utils =
    open System.IO    
    // Now you do not have to include the full paths.
    let writeToFile filename (text: string) =  
      let stream = new FileStream(filename, FileMode.Create)
      let writer = new StreamWriter(stream)
      writer.WriteLine(text)

Problem

I understand this is not possible in C#; the nearest thing is to form an alias at the top of the code file. Is there a way to do this in F#? See this question for the C# analogue of my question: My guess is "No, use an alias." but nothing ventured, nothing gained.

Original source

Related problems