Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a F# function in VB.NET

I have defined a F# function in my VB code as follow:

Dim g As Microsoft.FSharp.Core.FSharpFunc(Of Double, Double)

How can I assign g to a particular function: for example I want g(x) = x^2? What is the syntax for it in VB.NET ?

like image 651
N.T.C Avatar asked Apr 30 '26 09:04

N.T.C


1 Answers

In addition to what Reed says, the FSharp.Core library actually defines a helper method that will turn a delegate (which you can create using lambdas) into an F# function.

You can find it in FuncConvert.ToFSharpFunction see MSDN for more info. I would recommend using this function over creating a custom class, because it relies less on internal details of how the F# compiler compiles functions (you still need to know about the FSharpFunc type, but not as much...)

I don't know much about VB, but I suppose something like this might work:

Dim g = FuncConvert.ToFSharpFunction(Function(x As Double) x * x)
like image 198
Tomas Petricek Avatar answered May 01 '26 21:05

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!