For a quick explanation of what is going on, let's start here:
let getStringFromFile = File.OpenRead("c:\\eo\\raw.txt") |> fun s -> let r = new StreamReader(s) let data = r.ReadToEnd r.Close() s.Close() data
You can rewrite the first two lines of your function as:
let s = File.OpenRead(@"c:\eo\raw.txt")
Then you omitted the parentheses by this method:
let data = r.ReadToEnd r.Close() s.Close() data
As a result, data is of type unit -> string . When you return this value from your function, the whole result will be unit -> string . But look what happens between the assignment of a variable and its return: you have closed the threads.
The end result, when the user calls the function, the threads are already closed, which leads to the error that you see above.
And don't forget to remove your objects by declaring use whatever = ... instead of let whatever = ...
With that in mind, here's the fix:
let getStringFromFile() = use s = File.OpenRead(@"c:\eo\raw.txt") use r = new StreamReader(s) r.ReadToEnd()
source share