Dmi*_*nov 2 generics f# inline integer-division
为什么int64不支持LanguagePrimitives.DivideByInt?我认为写这样的东西是很自然的:
let inline DivBy2 n = LanguagePrimitives.DivideByInt n 2
let res = DivBy2 100L
Run Code Online (Sandbox Code Playgroud)
但编译器说int64 doesn't support the operator DivideByInt.
我试图欺骗:
type System.Int64 with
static member DivideByInt (n: System.Int64) (d: int) = n / (int64 d)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
通过int执行int64的泛型划分可以做些什么?
看看F#源代码,int64类型不包含在函数中DivideByInt,我不知道为什么.
您可以定义另一个通用函数,如下所示:
open LanguagePrimitives
type DivExtension = DivExtension of int with
static member inline (=>) (x , DivExtension y) = DivideByInt x y
static member (=>) (x:int64 , DivExtension y) = x / (int64 y)
static member (=>) (x:DivExtension, DivExtension y) = x
let inline DivByInt x y = x => DivExtension y
Run Code Online (Sandbox Code Playgroud)
或者您甚至可以隐藏原始DivideByInt功能:
let inline DivideByInt x y = x => DivExtension y
Run Code Online (Sandbox Code Playgroud)
请注意,您还可以添加更多重载(即对于int),在这种情况下,您不需要最后一个"虚拟"重载来推断正确的签名.