Using the Programming F# book, there’s an example first given as serial:
let result1 = longTask1()
let result2= = LongTask2()
Then using the PFX it is given to be
Open System.Threading.Tasks
let taskBody = new Func<string>(longTask1)
let task = Task.Factory.StartNew<string>(taskBody)
let result2 = longTask2()
let result1 = task.Result
However, in my case the function readBlock takes in a string filePath as argument and returns a seq. I tried like this
let taskBody = new System.Func<string, seq<_>>(readBlock)
let task = Task.Factory.StartNew<seq<_>>(taskBody.Invoke(filePath))
This is showing an error for the Task.Factory.StartNew portion – too many arguments. So how to go about calling parameterized functions that return a value?
The code in the book is quite obsolete. The book was written before the release of VS 2010, some parts of F# and PFX weren’t settled yet.
Now you can write:
The type checker will infer an appropriate type for
task, and you can invoketask.Resultto get results later on.There is implicit conversion between closure and
System.Func<_, _>, and using closure is more readable in this case.