2
votes

Any idea why F# can't compile the code?

let func(b: 'B, a: 'A  when 'A :> 'B) = b :?> 'A

There is a warning on 'A :> 'B and then an error on 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.

Update

Valid C# code:

static A func<B, A>(B b, A a) where A: B { return (A)b; }
1

1 Answers

2
votes

Looks like I hit this problem

How to constrain one type parameter by another

So F# doesn't support it.