简体   繁体   English

使用F#在类/接口中动态定义几个成员

[英]Define several members dynamically in class/interface using F#

I'm working on small program using F# just for the personal training and got some moment, which I can't solve. 我正在使用F#进行小型程序开发,仅用于个人培训,但花了点时间,我无法解决。

I'm describing some interface: 我正在描述一些接口:

type IСalculations =
    abstract member Add : int * int -> int
    abstract member Subtract : int * int -> int
    abstract member Multiply : int * int -> int
    abstract member Divide : int * int -> int

As you can see, the member signature besides the name is the same. 如您所见,除名称外,成员签名是相同的。

Am I able to do using F# the next (now will be pseudo-code ): 我能否在下一个中使用F#(现在将是伪代码 ):

let names = [ "Add", "Subtract", "Multiply", "Divide" ];
let ICalculations = new interface;

foreach ( name in names ) {
    ICalculations[ name ] : int * int -> int
}

The aim is NOT to repeat for each member the signature int * int -> int 目的不是要为每个成员重复签名int * int -> int

Is it possible? 可能吗?

You cannot define interface method types after the interface declaration. 接口声明后不能定义接口方法类型。 But you can define for example Dictionary, containing functions of your type: 但是您可以定义例如Dictionary,其中包含您类型的函数:

open System.Collections.Generic    
type Ops = Add | Subtract | Multiply | Divide
let pseudoInterface = Dictionary<Ops, int * int -> int>()

// Then somewhere in your program you could define this "methods"
pseudoInterface.[Add] <- fun (i, j) -> i + j
pseudoInterface.[Subtract] <- fun (i, j) -> i - j // etc...

Or you can define type alias for function type for brevity: 或者,为了简洁起见,您可以为函数类型定义类型别名:

type Op = int * int -> int
type IСalculations =
    abstract member Add : Op    
    abstract member Subtract : Op
    abstract member Multiply : Op
    abstract member Divide : Op

The only syntax for declaring an interface is : 声明接口的唯一语法是:

// Interface declaration:
[ attributes ]
type interface-name =
   [ interface ]     [ inherit base-interface-name ...]
     abstract member1 : [ argument-types1 -> ] return-type1
     abstract member2 : [ argument-types2 -> ] return-type2
     ...
   [ end ]

In the second line of your pseudo-code : 在伪代码的第二行:

let ICalculations = new interface;

you wish to use a let binding or equivalent. 您希望使用let绑定或等效绑定。 Unfortunately, let bindings only associate an identifier with a value or a function and not with a type or an interface. 不幸的是,让绑定仅将标识符与值或函数相关联,而不与类型或接口相关联。 So I am afraid there is no way. 因此,恐怕没有办法。 Other functional languages than F#, such as Idris, can do it. F#之外的其他功能语言(例如Idris)也可以做到。 If you are only bothered by the verbosity of having to repeat int*int->int, you can define a type alias like so : 如果您只被重复int * int-> int的冗长困扰,则可以定义类型别名,如下所示:

module T =
  type Op = int * int -> int

type IСalculations =
    abstract member Add : T.Op
    abstract member Subtract : T.Op
    abstract member Multiply : T.Op
    abstract member Divide : T.Op

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM