运算符使用seq<>>进行重载

时间:2017-03-29 16:23:31

标签: f# operator-overloading

在我正在试验的项目中,我不断地将序列元素相乘。我想创建一个这样的运算符:

let (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b

然而,在FSI中它返回:

val ( .* ) : a:seq<int> -> b:seq<int> -> seq<int>

以下失败:

  seq [1.0;2.0] .* seq [3.0;4.0];;
  -----^^^

stdin(16,6): error FS0001: This expression was expected to have type
    int    
but here has type
    float 

如何让操作员使用通用seq&lt;&#39; T&gt; (提供&#39; T支持乘法)?

1 个答案:

答案 0 :(得分:6)

您需要将 struct graph{ int count; int * array; } a_graph; int x = 10; MPI_Status status; //ONLY 2 RANKS ARE PRESENT. RANK 0 SENDS MSG TO RANK 1 if (rank == 0){ a_graph * my_graph = malloc(sizeof(my_graph)) my_graph->count = x; my_graph->array = malloc(sizeof(int)*my_graph->count); for(int i =0; i < my_graph->count; i++) my_graph->array[i] = i; MPI_Send(my_graph->array,my_graph->count,int,1,0,MPI_COMM_WORLD); free(my_graph->array); free(my_graph); } else if (rank == 1){ a_graph * my_graph = malloc(sizeof(my_graph)) my_graph->count = x; my_graph->array = malloc(sizeof(int)*my_graph->count); MPI_Recv(my_graph->array,my_graph->count,int,0,0,MPI_COMM_WORLD,&status) // MPI INVALID BUFFER POINTER ERROR HAPPENS AT THIS RECV } 关键字添加到运算符声明中,以便在编译时解析正确的重载:

inline
let inline (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b