F# 在接口成员实现中使用组合符号

F# use composition notation in interface member implementation

我有以下界面:

type IFactory<'TIn, 'TOut> =
  abstract Create: 'TIn -> 'TOut

我正在尝试编写一个 ComposedFactory。以下似乎是正确的语法,因为 VS 没有抱怨它:

type ComposedFactory<'TIn, 'TMid, 'TOut>
  (midFactory: IFactory<'TIn, 'TMid>,
   outFactory: IFactory<'TMid, 'TOut>) =

    let Create' =
        midFactory.Create >> outFactory.Create

    interface IFactory<'TIn, 'TOut> with
        member __.Create x = Create' x

但是我两次定义 "Create" 的事实感觉很愚蠢。我只想要一个接口。我该怎么做?

恐怕我不知道如何做你想做的事。我猜想接口函数的实现明确要求在其定义中指定参数。

我可以向您建议的最佳选择是滚边:

member __.Create x = x |> midFactory.Create |> outFactory.Create

你可以做运动:

type ComposedFactory<'TIn, 'TMid, 'TOut>
   (midFactory: IFactory<'TIn, 'TMid>,
    outFactory: IFactory<'TMid, 'TOut>) =

interface IFactory<'TIn, 'TOut> with
    member __.Create x = (midFactory.Create >> outFactory.Create) x

但无论如何我都不能说它比你以前拥有的更可取。