我创建了以下运算符来帮助安全划分。
let (/!) a b =
if b = 0 then 0
else a / b
问题是它只适用于整数,我希望这个函数可以处理任何数字原语(int,float,decimal等)。
我已经完成了关于自动概括的一些阅读,但它并没有完全沉沦,而且我不确定这是否是正确的方向。
如何完成此操作符的推广?
谢谢,
乔
答案 0 :(得分:8)
嗨,这是一个隐藏的宝石,但你要找的是:
let inline (/!) (a : ^a) (b : ^a) : ^a =
if b = LanguagePrimitives.GenericZero
then LanguagePrimitives.GenericZero
else a / b
btw:这有这种怪物类型:
val inline ( /! ) :
a: ^a -> b: ^a -> ^a
when ^a : equality and ^a : (static member get_Zero : -> ^a) and
^a : (static member ( / ) : ^a * ^a -> ^a)
(这就是为什么我真的不想在声明中写这个;))
正如你所看到的那样,支持泛型数字代码,但并不经常讨论(F#有一些类似于类的东西 - 这是一个例子,其他类似于comparable
等等。)
Tomas写了一篇关于此的好文章:Writing generic numeric code
PS你也不需要^a
- 但我喜欢写签名 - 即使你可以这样做:
let inline (/!) a b =
if b = LanguagePrimitives.GenericZero
then LanguagePrimitives.GenericZero
else a / b
val inline ( /! ) :
a: ^a -> b: ^b -> ^c
when ( ^a or ^b) : (static member ( / ) : ^a * ^b -> ^c) and
^b : (static member get_Zero : -> ^b) and ^b : equality and
^c : (static member get_Zero : -> ^c)
它对你没有好处,因为真正的除法运算符通常只对两个参数都有一种类型 - 正如我所说:我喜欢强调参数名称的类型;)
你可以绕过GenericZero
这样的事情:
> let inline (/!) a b = if b = (b-b) then (b-b) else a/b;;
val inline ( /! ) :
a: ^a -> b: ^b -> ^b
when ( ^a or ^b) : (static member ( / ) : ^a * ^b -> ^b) and
^b : (static member ( - ) : ^b * ^b -> ^b) and ^b : equality
(为了安全起见:你可能会遇到某些类型/数字的问题;)