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

时间:2018-11-17 20:47:56

标签: interface f# function-composition

我有以下界面:

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

但是我两次定义“创建”的事实让人感到愚蠢。我只想要一个接口。我该怎么办?

2 个答案:

答案 0 :(得分:3)

您可以在运动中这样做:

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

但是我不能说它比您以前拥有的更可取。

答案 1 :(得分:2)

恐怕我不知道该怎么做。我猜想接口函数的实现显式要求在其定义中指定参数。

我可以向您建议的最佳替代方法是管道输送

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