在F#允许的歧视联盟中相互引用的案例?

dev*_*ium 2 .net f# functional-programming discriminated-union

以下歧视联盟未能编译:

type Expression =
  | Identifier of string
  | Integer of int
  | Assignment of Identifier * Expression
Run Code Online (Sandbox Code Playgroud)

显示的错误是

The type "Identifier" is not defined.
Run Code Online (Sandbox Code Playgroud)

关于最后一个工会案件.

我尝试Expression使用rec属性进行标记,但这似乎无济于事.

有解决办法吗?更好的是,引用我的麻烦的原因是什么?

Bri*_*ian 10

听起来你真的想要

| Assignment of string * Expression
Run Code Online (Sandbox Code Playgroud)

或者

type Id = Id of string
type Expression =
    | Identifier of Id
    | Integer of int
    | Assignment of Id * Expression
Run Code Online (Sandbox Code Playgroud)

如果你想要一个额外的名义类型的所有标识符.

最好在可能的情况下排除类型系统中无意义的值(无意义的状态不应该是可表示的),这就是为什么Expression如果你的语言不需要,我会避免在作业的左侧.

在任何情况下,不允许的原因是因为Identifier不是类型(Expression是).