递归对象:单向工作,但不工作

时间:2013-05-03 14:40:44

标签: recursion ocaml

下面是两个例子,它们非常简化,并且这种简化形式没有实际意义,但它有助于我理解使用这两者的工作原理:

let test x = object (self)
  val x = x
  method testme = x == self
end in
Printf.printf "printme: %b\n" (test test)#testme;;

let test x = object
  method testme = (==) x
end in
Printf.printf "printme: %b\n" ((test test)#testme test);;

第一个例子不起作用,而第二个例子不起作用。第一个论点是关于xtest的类型不相容,但我不明白它是如何得出x的类型是< testme : bool > -> < testme : bool >的结论。为什么不只是< testme : bool >

1 个答案:

答案 0 :(得分:2)

这就是我所看到的:

# let test x = object (self) method testme = x == self end;;
val test : < testme : bool > -> < testme : bool > = <fun>

实际上,test是一个函数,而不是一个对象。它具有基本OCaml函数定义的形式。您说x是一个函数,但xtest的函数参数。因此x的类型为< testme: bool >

相关问题