简体   繁体   English

在C#中定义F#'**'运算符

[英]Define F# '**' operator in C#

I see that F# uses the ** operator for powers, so 2 ** 5 = 32 . 我看到F#使用**运算符作为幂,所以2 ** 5 = 32 This is different from C#, where you have the option to use the '^' operator in your custom types, but for some reason isn't used by the built in number types. 这与C#不同,您可以选择在自定义类型中使用“^”运算符,但由于某些原因,内置数字类型不使用它。

But how do you implement the ** operator in C# for use in an F# project? 但是如何在C#中实现**运算符以用于F#项目?

If I do this in C#: 如果我在C#中这样做:

public static Integer operator ^(Integer left, Integer right)
{
    if (Integer.IsNaN(left) || Integer.IsNaN(right)) return NaN;
    return left.RaiseToPower(right);
}

It compiles fine, and I can use it the same way as the '+' operator, but neither of these work in F#: 编译很好,我可以像'+'运算符一样使用它,但这些都不适用于F#:

let intgr3 = intgr1 ** intgr2

let intgr3 = intgr1 ^ intgr2

And in C#, this doesn't work: 在C#中,这不起作用:

public static Integer operator **(Integer left, Integer right)
{
    if (Integer.IsNaN(left) || Integer.IsNaN(right)) return NaN;
    return left.RaiseToPower(right);
}

So how do I define the F# equivalent of the ** operator in C#? 那么如何在C#中定义**运算符的F#等价物呢?
Thanks. 谢谢。

As I said in my comments, C# does not let you define new operators and ** is not an operator in C#. 正如我在评论中所说,C#不允许您定义新运算符, **不是C#中的运算符。 ^ is an operator, however it is the logical XOR operator, NOT the exponentiation operator. ^是一个运算符,但它是逻辑XOR运算符,而不是取幂运算符。 This gave me a hint that F# might be translating your C# operator into native F# ( ^^^ for logical XOR). 这给了我一个提示,F#可能会将你的C#操作符转换为本机F#(逻辑XOR为^^^ )。

So, I created a couple test projects and using your definition of ^ , here's what I found in F#: 所以,我创建了几个测试项目并使用你的定义^ ,这是我在F#中找到的:

open CSLib // CSLib is the C# library

let ( ** ) (x : Integer) (y : Integer) = x.RaiseToPower y

let x = new Integer()
let y = new Integer()

let a = x ^ y // error
let b = x ^^^ y // compiles, but looks like XOR
let c = x ** y // compiles

You can define new global operators in F#, however if you want this to be a general library that may not be acceptable. 您可以在F#中定义新的全局运算符,但是如果您希望这是一个可能不可接受的通用库。

You can define the Exponentiation operator for use in F# by defining a public static method Pow in the Integer type in your C# library: 您可以通过在C#库中的Integer类型中定义公共静态方法Pow来定义在F#中使用的Exponentiation运算符:

public static Integer Pow(Integer left, Integer right)
{
    if (Integer.IsNaN(left) || Integer.IsNaN(right)) return NaN;
        return left.RaiseToPower(right);
}

Then, you are able to use it directly in F# as ** . 然后,您可以直接在F#中使用它作为** I will note that overloaded operators in C# are not idiomatic so having a Pow method will seem quite natural to C# users. 我会注意到C#中的重载运算符不是惯用的,所以对于C#用户来说,使用Pow方法看起来很自然。

I believe the reason why you can't define a ** operator for your object is that this is not one of the operators supported by the language. 我相信你不能为你的对象定义一个**运算符的原因是这不是该语言支持的运算符之一。 It's not a recognized operator.. Why not just use the Math library http://msdn.microsoft.com/en-us/library/system.math.pow.aspx 它不是一个公认的运算符..为什么不使用数学库http://msdn.microsoft.com/en-us/library/system.math.pow.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM