代码之家  ›  专栏  ›  技术社区  ›  WeNeedAnswers

在F#中使用哪个抽象类或接口?

f#
  •  2
  • WeNeedAnswers  · 技术社区  · 14 年前

    2 回复  |  直到 14 年前
        1
  •  5
  •   Tomas Petricek    14 年前

    我认为接口的使用频率往往比抽象类要高很多(与C#等面向对象语言相比)。

    在许多情况下,您不需要这两者中的任何一个,因为您只需要编写高阶函数(将函数作为参数,而不是将接口作为参数)。但是,有时您可能有两个始终一起使用的函数—在这种情况下,您可以将两个函数分组到一个接口中:

    // Instead of using higher-order function
    val foo : (int -> string) -> (string -> int) -> ...
    
    // ..we can define an interface
    type TwoWayConversion = 
      abstract ToString : int -> string
      abstract FromString : string -> int
    
    val foo : TwoWayConversion -> ...
    

    功能性风格

        2
  •  3
  •   Mau    14 年前

    好吧,如果你在抽象类和接口之间争论的话,我认为你这样或那样的理由和在C#中是一样的。 如果您需要编写一个F#库以便在C#中使用,那么您可能需要为导出的类型使用接口等。