F#警告:此构造导致代码不像类型注释所指示的那样通用

时间:2015-08-07 03:41:01

标签: generics f#

知道为什么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; }

1 个答案:

答案 0 :(得分:1)

看起来我遇到了这个问题

How to constrain one type parameter by another

所以F#并不支持它。