F#3.1:为什么我不能使用具有显式通用成员方法的管道运算符?

时间:2017-01-09 19:19:55

标签: generics f#

给出以下代码:

type MyDU =
| B of bool
| I of int
| S of string

type Ops () =
    static member myFn<'T> x =
        let v =
            match x with
            | B b -> box b
            | I i -> box i
            | S s -> box s

        match v with
        | :? 'T as y -> Some y
        | _ -> None

...以下产生error FS0717: Unexpected type arguments

I 7 |> Ops.myFn<int>

Ops.myFn<int> <| I 7

但是,这很好用:

Ops.myFn<int> (I 7)

另外,如果我在模块中定义myFn,我没有错误:

let myFn<'T> x =
    let v =
        match x with
        | B b -> box b
        | I i -> box i
        | S s -> box s

    match v with
    | :? 'T as y -> Some y
    | _ -> None

I 7 |> myFn<int>  // Works fine

解释

0 个答案:

没有答案