如何使用递归类型的显式成员约束

时间:2018-03-26 08:27:38

标签: f# recursive-type

是否可以对递归类型使用显式成员约束?

以下工作正常(非递归):

type Wrapper< ^a when ^a : (static member Baz: string) > = Wrapper of ^a

type Foo =
  { Str: string }
  static member Baz = ""

type Bar =
  { Foo: Wrapper<Foo> }
  static member Baz = ""

问题是我还需要Foo类型Wrapper<Bar>上的字段。但是,一旦我将type Bar更改为and Bar以启用此功能(或使整个模块递归),我会收到以下错误:

type Wrapper< ^a when ^a : (static member Baz: string) > = Wrapper of ^a

type Foo =
  { Str: string }
  static member Baz = ""

and Bar =
  { Foo: Wrapper<Foo> }
//       ^^^^^^^^^^^^  The type 'Foo' does not support the operator 'get_Baz'
  static member Baz = ""

是否可以对递归类型使用显式成员约束?

0 个答案:

没有答案