F#和运算符重载:(>)和(^) [英] F# and Operator Overloads: (>) and (^)

查看:110
本文介绍了F#和运算符重载:(>)和(^)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,有人可以向我解释为什么F#允许您重载>和^运算符,但不允许您使用它们吗?

Ok, so can someone explain to me why F# allows you to overload the > and ^ operators, but doesn't allow you to use them?

+ (op_Addition): Works just fine.
^ (op_Concatenate): Compiler error in F#. Apparently only strings can be concatenated.
> (op_GreaterThan): Runtime Error – Failure during generic comparison: the type Program+OppTest4 does not implement the System.IComparable interface.

如果我将F#代码编译为库并使用VB中的那些运算符,则它们都可以工作.如果我使用C#中的那些运算符,则除op_Concatenate之外的所有工作都如预期的那样.但是F#不仅会忽略其中的一些,静态类型检查器甚至不会费心地告诉您它打算这样做.

If I compile my F# code as a library and use those operators from VB, they all work. If I use those operators from C#, all but op_Concatenate work (as expected). But F# not only ignores some them, the static type checker doesn't even bother telling you that it plans on doing so.

编辑代码示例

type OppTest4(value: int) =
   member this.value = value
   static member (^) (left : OppTest4, right : OppTest4) =
     OppTest4( Int32.Parse( left.value.ToString() ^ right.value.ToString()  ))
   static member (+) (left : OppTest4, right : OppTest4) =
     OppTest4(left.value + right.value )
   static member (>) (left : OppTest4, right : OppTest4) =
     left.value > right.value
   static member (<) (left : OppTest4, right : OppTest4) =
     left.value < right.value

推荐答案

F#具有这些对于F#合理的运算符的默认含义.您总是可以定义自己的含义来遮盖默认值,例如la

F# has default meanings for these operator symbols that's reasonable for F#. You can always define your own meanings that shadow the defaults, a la

let (>) x y = ...

例如,您可以将此运算符定义为"T.operator>(U)"(假设x的类型为T,y的类型为U).

For example, you could define this operator to mean "T.operator>(U)" (assuming x has type T and y has type U).

有关默认定义,请参见源代码发行版中FSharp.Core中的prim-types.fs. (它们是不平凡的!)

See prim-types.fs in FSharp.Core in the source distribution for the default definitions. (They are non-trivial!)

综合考虑以下因素:(1)缺乏对CLR上类似类型类机制的支持(用于在一组其他不相关的类型之间定义通用语义)和(2)原始类型(如'int ')对于任何编程语言实现(例如System.Int32都没有定义operator +方法,但是大多数编程语言都选择表现得像存在这样的方法)通常都需要特殊情况,很难想象任何通常可互操作的运算符.Net上所有语言的版本.在设计上需要权衡取舍,具体取决于语言选择要做什么(这里有太多相互影响的问题要总结).在任何情况下,您都应该能够从F#中调用任何方法,并且如果不希望使用默认的运算符行为,则可以将运算符重新定义(阴影)为所需的行为.如果您有某种特殊的情况需要考虑,请告诉我.

Given the combination of (1) lack of support for a type-class like mechanism on the CLR (for defining common semantics among a set of otherwise-unrelated types) and (2) the fact that primitive types (like 'int') often need to be special-cased for any programming language implementation (e.g. System.Int32 does not define an operator+ method, but most programming languages choose to behave as though such a method exists), it's hard to imagine any generally interoperable operator stuff across all languages on .Net today. There are a lot of design-trade-offs depending on exactly what a language chooses to do (too many interacting issues to sum up here). In any case, you should be able to call any method from F#, and if the default operator behaviors are undesirable, you can redefine (shadow) the operators to the behaviors you want. If there's a particular scenario you have in mind that you're having trouble to make work, let me know.

编辑

我在以下位置添加了更多细节

I added more detail at

http://cs.hubfs.net/forums/thread/10869.aspx

这篇关于F#和运算符重载:(&gt;)和(^)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆