我正在尝试通过使用编译代码而不是顶级代码来学习 OCaml;然而,网上的大部分示例代码似乎更适合后者。
我想在下面的对象的方法中创建一个新的 Foo 。此代码无法编译,引用 doFooProc 定义的语法错误。
class bar =
object (self)
method doFooProc = (new Foo "test")#process
end;;
class foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end;;
此外,“let”语法在类定义中似乎并不友好。这是为什么?
class bar =
object (self)
method doFooProc =
let xxx = (new Foo "test");
xxx#process
end;;
class foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end;;
如何在 doFooProc 方法中创建类 foo 的新对象并调用实例化的 foo 的 process 命令?