F# 过于激进的类型推断?

标签 f# pattern-matching type-inference

所以在做一些Project Euler问题,我希望能够取整数值(int、long、bigint 等)的平方根,但 Sqrt 仅针对浮点值定义。所以我一直在写我自己的小 Newton-Raphson 算法,它非常准确,可以满足我的需要。但是,我希望能够对浮点值调用内置的 sqrt 函数。所以我写了这样的东西:

let inline dsqrt x =
    match box x with
    | :? float -> sqrt x
    | :? float32 -> sqrt x
    | _ -> p_dsqrt x

显然,我的函数名为“p_dsqrt”。然而,这个函数要求输入定义了一个 Sqrt 方法,这有点违背了整个目的。我是否缺少某种类型约束,或者什么?

最佳答案

如果要使用匹配,则不需要 inline 关键字,但如果要使用内联函数和“帽子类型”,请使用重载而不是匹配:

type Sqrt = Sqrt with
    // Dummy overload in order to get the right types inferred (will never reach here)
    static member inline ($) (Sqrt, _:^t when ^t:null and ^t: struct) = id

    // Existing sqrt
    static member inline ($) (Sqrt, x:'a) :'a = sqrt x 

    // Your Newton-Raphson based sqrt's
    static member        ($) (Sqrt, x:int   ) = sqrtForInt    x
    static member        ($) (Sqrt, x:bigint) = sqrtForBigInt x 

let inline sqrt (x:'t) :'t = Sqrt $ x 

返回类型将始终与输入类型相同,选择的 sqrt 的实现将取决于该类型。此选择将在编译时发生,这是与在运行时解决的 match 方法的主要区别。

如果我去掉虚拟重载,它会和你的代码有同样的问题:它需要 sqrt 约束。

关于F# 过于激进的类型推断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12572471/

相关文章:

F#:减少此功能

Python 正则表达式匹配 IP 地址与/CIDR

regex - split_to_table() 按字符数,无定界符

haskell - haskell中的函数组合类型推断

c++ - 是否可以模拟模板<auto X>?

asynchronous - 使用 Async 和 Option 的 F# 程序流程问题

generics - 当组合泛型和非泛型类时,类型变量转义作用域

f# - 使用 TryParse 重载解决方案

c++ - 找到字符串模式的更好解决方案?

vb.net - 为什么 VB.Net 类型推断在类字段中不起作用?