How to constrain one type parameter by another

后端 未结 1 844
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 07:05

Is there a way to constrain one type parameter to be derived from another?

type Foo<\'T, \'U when \'U :> \'T> = 
    member x.Bar() : \'T = upcast Un         


        
相关标签:
1条回答
  • 2020-11-27 07:44

    No :(. This is one of the most unfortunate limitations of F# at the moment (in my opinion). See the Solving Subtype Constraints section of the spec, which states that

    New constraints of the form type :> 'b are solved again as type = 'b.

    This is really a shame since otherwise we could work around F#'s lack of generic variance:

    let cvt<'a,'b when 'a :> 'b> (s:seq<'a>) : seq<'b> = // doesn't compile
      s |> box |> unbox
    
    0 讨论(0)
提交回复
热议问题