给出以下代码:
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
解释