3

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.

Community
  • 1
  • 1
Robino
  • 4,530
  • 3
  • 37
  • 40

1 Answers1

5

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)
pad
  • 41,040
  • 7
  • 92
  • 166