I am attempting to learn OCaml by using compiled code instead of the top-level; however, much of the sample code online seems to appeal to the latter.
我试图通过使用编译代码而不是*来学习OCaml;但是,大多数在线示例代码似乎都对后者有吸引力。
I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition.
我想在下面的对象的方法中创建一个新的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;;
Additionally, the "let" syntax don't seem to be friendly within class definitions. Why is that?
另外,“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;;
How do I go about creating a new object of class foo in the doFooProc method and call the instantiated foo's process command?
如何在doFooProc方法中创建类foo的新对象并调用实例化的foo的进程命令?
2 个解决方案
#1
You're mostly correct, but are either confusing syntax with the module system, or thinking of other languages. Take my considerations and you should be good!
你大多是正确的,但要么是模糊系统的语法混乱,要么是考虑其他语言。考虑一下你应该做得好!
I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition.
我想在下面的对象的方法中创建一个新的Foo。此代码无法编译,引用了doFooProc定义的语法错误。
Lowercase "foo" for objects, modules are uppercase. Also, you must put the definition of foo above the object that calls it. You should get an Unbound class foo
if this happens.
对象的小写“foo”,模块是大写的。此外,您必须将foo的定义放在调用它的对象之上。如果发生这种情况,你应该得到一个未绑定的类foo。
class bar =
object (self)
method doFooProc = (new foo "test")#process
end;;
Additionally, the "let" syntax don't seem to be friendly within class definitions. Why is that?
另外,“let”语法在类定义中似乎不友好。这是为什么?
Because you don't have a matching in
, instead you have a semi-colon. Then it will work. Also, you can remove those extra parens, but it doesn't matter.
因为你没有匹配,而是你有一个分号。然后它会工作。此外,你可以删除那些额外的parens,但这没关系。
class bar =
object (self)
method doFooProc =
let xxx = (new Foo "test") in
xxx#process
end;;
If, say, a method in foo instantiated a bar as well, is there any way to escape the problem that arises with ordering the class definitions within the source file?
例如,如果foo中的方法实例化了一个条形码,有没有办法逃避在源文件中排序类定义时出现的问题?
Yes. It's just like writing mutually recursive functions and modules, you connect them with the and
keyword.
是。这就像编写相互递归的函数和模块一样,用关键字连接它们。
class bar =
object (self)
method doFooProc = (new foo "test")#process
end
and foo (param1:string) =
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end
#2
For two mutually recursive class, use the and keyword
对于两个相互递归的类,请使用and关键字
class bar =
object (self)
method doFooProc =
let xxx = (new foo "test") in
xxx#process
end
and foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
method bar = new bar
end;;`
#1
You're mostly correct, but are either confusing syntax with the module system, or thinking of other languages. Take my considerations and you should be good!
你大多是正确的,但要么是模糊系统的语法混乱,要么是考虑其他语言。考虑一下你应该做得好!
I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition.
我想在下面的对象的方法中创建一个新的Foo。此代码无法编译,引用了doFooProc定义的语法错误。
Lowercase "foo" for objects, modules are uppercase. Also, you must put the definition of foo above the object that calls it. You should get an Unbound class foo
if this happens.
对象的小写“foo”,模块是大写的。此外,您必须将foo的定义放在调用它的对象之上。如果发生这种情况,你应该得到一个未绑定的类foo。
class bar =
object (self)
method doFooProc = (new foo "test")#process
end;;
Additionally, the "let" syntax don't seem to be friendly within class definitions. Why is that?
另外,“let”语法在类定义中似乎不友好。这是为什么?
Because you don't have a matching in
, instead you have a semi-colon. Then it will work. Also, you can remove those extra parens, but it doesn't matter.
因为你没有匹配,而是你有一个分号。然后它会工作。此外,你可以删除那些额外的parens,但这没关系。
class bar =
object (self)
method doFooProc =
let xxx = (new Foo "test") in
xxx#process
end;;
If, say, a method in foo instantiated a bar as well, is there any way to escape the problem that arises with ordering the class definitions within the source file?
例如,如果foo中的方法实例化了一个条形码,有没有办法逃避在源文件中排序类定义时出现的问题?
Yes. It's just like writing mutually recursive functions and modules, you connect them with the and
keyword.
是。这就像编写相互递归的函数和模块一样,用关键字连接它们。
class bar =
object (self)
method doFooProc = (new foo "test")#process
end
and foo (param1:string) =
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end
#2
For two mutually recursive class, use the and keyword
对于两个相互递归的类,请使用and关键字
class bar =
object (self)
method doFooProc =
let xxx = (new foo "test") in
xxx#process
end
and foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
method bar = new bar
end;;`