我有两个类定义如下:
type foo () =
let getBar() =
new bar()
type bar () =
let getFoo() =
new foo()
Run Code Online (Sandbox Code Playgroud)
编译器说
The type 'bar' is not defined
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
这是使用and关键字的地方(必须在同一个文件中很明显):
type Foo () =
let getBar () =
Bar ()
and Bar () =
let getFoo () =
Foo ()
Run Code Online (Sandbox Code Playgroud)
如果您有相互递归函数(甚至值),您也可以使用它 - 但您必须rec在第一个绑定中添加关键字:
let rec f x = if x > 0 then g x else 0
and g x = f (x-1)
Run Code Online (Sandbox Code Playgroud)