1
我想用ocamlc编译下面的代码。OCaml变量类型泛化
type 'a splitter = {name: string; decision_function : 'a -> bool}
let splitter12a = {name="x1>x2"; decision_function=(fun x -> x.(1)>x.(2))};;
let generate_splitter i j = {name="x"^string_of_int(i)^">x"^string_of_int(j); decision_function=(fun x -> x.(i) > x.(j))} ;;
let splitter12 = generate_splitter 1 2;;
然而,编译器会抱怨:
File "error.ml", line 7, characters 17-38:
Error: The type of this expression, '_a array splitter,
contains type variables that cannot be generalized
我不明白为什么我可以宣布一个专门splitter12a
而generate_splitter
不能生成专门的分离器。
谢谢!什么是最体面的解决方法?我不熟悉“eta扩展”并专门研究splitter(我现在使用的解决方法)不是一个可行的解决方案 – RUser4512
确实没有直接的解决方法,但请参阅我的编辑。 –