I made a function in F#
let tryParseArray tryParse (separator:char) (line: string) =
// inside the function I use the tuple form of tryParse
It works fine if I call it in such way: tryParseArray Int32.TryParse ',' "2,3,2,3,2"
Now I would like this function to be available in C# as well, so I did this:
static member TryParseArray (line, tryParse, separator) =
line |> tryParseArray tryParse separator
Then I realized that TryParseArray actually takes tryParse argument as FSharpFunc, which is not friendly to C# at all, so I tried this:
static member TryParseArray (line, [<Out>] tryParse: (string * byref<'a> -> bool), separator) =
line |> tryParseArray tryParse separator
but now tryParseArray doesn’t accept tryParse as a valid argument (type error)
What should I do?
I wish in C# I can call TryParseArray("2,3,2,3,2", Int32.TryParse, ',') as well
You can expose this function to C# using custom delegate type :
EDITED
NOTE: on the C# side you’ll need to specify type parameter for TryParse explicitly (Why don’t anonymous delegates/lambdas infer types on out/ref parameters?)