如何在泛型函数中定义F#泛型函数参数?

时间:2016-05-08 08:34:15

标签: generics f#

let getLogicalWithDoubleLevelElement<'E when 'E :> DoubleLevelElement>
    (logicalModifier : InletPosition * OutletPosition -> Logical)
    (logical : Logical) =

我需要logicalModifier为泛型函数,其类型与getLogicalWithDoubleLevelElement类型相同。

如何定义?

1 个答案:

答案 0 :(得分:1)

如果需要将显式多态函数作为参数传递,那么在F#中执行此操作的唯一方法是使用具有泛型方法的新类型:

type LogicalModifier =
    abstract MakeLogical<'E when 'E :> DoubleLevelElement> : InletPosition * OutletPosition -> Logical

let getLogicalWithDoubleLevelElement<'E when 'E :> DoubleLevelElement>
    (logicalModifier : LogicalModifier)
    (logical : Logical) = ... logicalModifier.MakeLogical<'E> ...