如何在f#中声明循环依赖的抽象类

时间:2021-10-05 19:43:36

Consider the two abstract classes alpha and beta:

考虑两个抽象类:

[<AbstractClass>]  
type alpha () =
    abstract member foo: beta->beta

[<AbstractClass>] 
and beta () = //***
    abstract member bar: alpha

If I try to compile that I get an error, on the line indicated with * * *:

如果我试图编译它,就会得到一个错误,在* * *:

error FS0010: Unexpected keyword 'and' in interaction

And if I write:

如果我写:

[<AbstractClass>]  
type alpha () =
    abstract member foo: beta->beta

and beta () =
    abstract member bar: alpha

then I get:

然后我得到:

error FS0365: No implementation was given for 'abstract member beta.bar : alpha'

and the hint that I should add the AbstractClass Attribute

以及应该添加AbstractClass属性的提示

So how do i declare circularly defined abstract classes?

那么,如何声明循环定义的抽象类呢?

1 个解决方案

#1


7  

Put the attribute after the 'and' keyword:

将属性放在'and'关键字之后:

[<AbstractClass>]
type alpha () =
    abstract member foo : beta -> beta

and [<AbstractClass>]  beta () =
    abstract member bar : alpha

#1


7  

Put the attribute after the 'and' keyword:

将属性放在'and'关键字之后:

[<AbstractClass>]
type alpha () =
    abstract member foo : beta -> beta

and [<AbstractClass>]  beta () =
    abstract member bar : alpha