相关疑难解决方法(0)

内联函数和类型扩展

考虑我有两种不同的库类型:

type Foo = { foo : string }
type Bar = { bar : int32 }
Run Code Online (Sandbox Code Playgroud)

我想实现zoo适用于任一个Foo或Bar实例的泛型函数.我无法改变Foo,Bar因为它们是图书馆代码的一部分.

下面是使用类型扩展和内联函数的解释了我的第一次尝试在这里:

// Library.fs
module Library

type Foo = { foo : string }
type Bar = { bar : int32 }

// Program.fs
type Foo with
    static member zoo (f : Foo) = "foo"

type Bar with
    static member zoo (b : Bar) = "bar"

let inline …
Run Code Online (Sandbox Code Playgroud)

f# inline

7
推荐指数
1
解决办法
194
查看次数

F#:不一致的SRTP静态扩展方法类型匹配

我试图使用PrintfFormat类型解析器的强制解析,它最初似乎适用,int但随后的相同方法string却不起作用...虽然float起作用了,所以我认为是Value / Ref类型问题,但后来尝试了bool,但没有成功。像String一样工作。

int与float工作,string与bool不!?

(ParseApply方法目前是虚拟实现)

    type System.String  with static member inline ParseApply (path:string) (fn: string -> ^b) : ^b = fn ""
    type System.Int32   with static member inline ParseApply (path:string) (fn: int   -> ^b) : ^b = fn 0
    type System.Double  with static member inline ParseApply (path:string) (fn: float -> ^b) : ^b = fn 0.
    type System.Boolean with static member inline ParseApply (path:string) (fn: …
Run Code Online (Sandbox Code Playgroud)

extension-methods f# constraints

4
推荐指数
2
解决办法
192
查看次数

非惯用全局运算符重载如何工作?

我想了解这个答案的代码

type Mult = Mult with
    static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> 
        v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b>
    static member inline ($) (Mult, v1:'a      ) = fun (v2:'a) -> v1 * v2 :'a

let inline (*) v1 v2 = (Mult $ v1) v2
Run Code Online (Sandbox Code Playgroud)

F#可以解决重载成员.(因为它不支持成员的currying).所以,我认为,它也适用于方法

但它没有:

type Mult = Mult with
        static member inline Do (Mult, v1: 'a list) …
Run Code Online (Sandbox Code Playgroud)

f# overloading operator-overloading operators

3
推荐指数
1
解决办法
92
查看次数