知道为什么F#无法编译代码?
let func(b: 'B, a: 'A when 'A :> 'B) = b :?> 'A
'A :> 'B
上有警告,b :?> 'A
上有错误:
stdin(16,29): warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'A has been constrained to be type ''B'.
let func(b: 'B, a: 'A when 'A :> 'B) = b :?> 'A;;
----------------------------------------^^^^^^^^
stdin(16,41): warning FS0067: This type test or downcast will always hold
let func(b: 'B, a: 'A when 'A :> 'B) = b :?> 'A;;
----------------------------------------^^^^^^^^
stdin(16,41): error FS0008: This runtime coercion or type test from type
'B
to
'B
involves an indeterminate type based on information prior to this program point. Runtime type tests are not allowed on some types. Further type annotations are needed.
更新
有效的C#代码:
static A func<B, A>(B b, A a) where A: B { return (A)b; }