在C#中使用自定义F#运算符?

时间:2022-09-01 09:37:08

I've stumbled upon the fact that it's possible to define custom operators in F#. Also, I believe it's possible to reuse F# code in C#.

我偶然发现可以在F#中定义自定义运算符这一事实。另外,我相信可以在C#中重用F#代码。

Is it possible to create a custom operator in F#, reference the project in C# and then reuse the operator in C#? For example, lets say I were to define the .? operator as something in F#, could I then somehow reuse it in my C# projects?

是否可以在F#中创建自定义运算符,在C#中引用项目,然后在C#中重用运算符?例如,假设我要定义。?运算符作为F#中的东西,我可以在C#项目中以某种方式重用它吗?

1 个解决方案

#1


16  

Custom defined operators can be used just fine from C#. The names are auto-generated and are of the form op_<symbol...> (see Overloaded Operator Names on MSDN).

自定义运算符可以在C#中正常使用。这些名称是自动生成的,格式为op_ (请参阅MSDN上的重载运算符名称)。

For example

let (|?) a b = ...

would be available as op_BarQmark.

将作为op_BarQmark提供。

However, as Mau points out in his comment, you will only be able to use it from C# as a method, not an operator.

但是,正如Mau在评论中指出的那样,你只能将它从C#用作方法,而不是操作符。

#1


16  

Custom defined operators can be used just fine from C#. The names are auto-generated and are of the form op_<symbol...> (see Overloaded Operator Names on MSDN).

自定义运算符可以在C#中正常使用。这些名称是自动生成的,格式为op_ (请参阅MSDN上的重载运算符名称)。

For example

let (|?) a b = ...

would be available as op_BarQmark.

将作为op_BarQmark提供。

However, as Mau points out in his comment, you will only be able to use it from C# as a method, not an operator.

但是,正如Mau在评论中指出的那样,你只能将它从C#用作方法,而不是操作符。