1

我做了一个类方法,我想要这种类型:

unit -> (dir -> 'b)

但我的实际方法:

method iter () = fun x -> match x with
| Up -> if (Stack.is_empty pz) then raise Stack.Empty else if (Stack.length pz = 1) then failwith "Cannot go up" else (ignore (Stack.pop pz) ; {< a = (Stack.top pz) >})
| Down(v) -> match (Stack.top pz) with
| Noeud(o, {contents = []}) -> raise Not_found
| Noeud(o, {contents = l}) -> if mem_assoc v l then ((Stack.push (assoc v l) pz) ; {< a = (Stack.top pz) >} ) else raise Not_found

有类型unit -> dir -> 'b

我怎样才能使它成为第一种类型?

以下是自定义类型:

    type 'a arbre =  Noeud of 'a option ref * (char * 'a arbre) list ref
    type dir = Up | Down of char

编辑:我需要这个,所以它可以符合某个接口,并且由于类型不匹配,它不会编译。谢谢!

4

1 回答 1

6

这不是问题。unit -> (dir -> 'b)unit -> dir -> 'bOCaml 中的类型相同!(类型箭头是右结合的)

您能否向我们展示实际的错误消息,以便我们知道问题出在哪里?

附录:你真的试过这个吗?如果没有其他问题,那么您会发现它会正常工作。

于 2012-04-26T03:27:36.627 回答