阅读Array2D模块的源代码,我在许多核心函数的实现中偶然发现了这个有趣的结构,例如:
[<CompiledName("Get")>]
let get (array: 'T[,]) (n:int) (m:int) = (# "ldelem.multi 2 !0" type ('T) array n m : 'T #)
Run Code Online (Sandbox Code Playgroud)
我只能假设这是内联CIL的语法,这里使用它显然是为了获得性能优势.但是,当我尝试在程序中使用此语法时,出现错误:
warning FS0042: This construct is deprecated: it is only for use in the F# library
Run Code Online (Sandbox Code Playgroud)
究竟是什么?有没有详细的文件?
我想使用静态解析的类型参数和我添加到内置类型的一些扩展方法,比如float32和int32,所以我尝试了以下方法:
module Foo =
type System.Single with
static member Bar x = x + 1.0f
let inline bar (x : ^a) =
(^a : (static member Bar : ^a -> ^a) x)
open Foo
[<EntryPoint>]
let main argv =
System.Console.WriteLine (bar 1.0f) (* Compilation fails here *)
0
Run Code Online (Sandbox Code Playgroud)
编译器抱怨说The type 'float32' doesn't support the operator 'Bar'.我究竟做错了什么?