受歧视的工会可以相互提及吗?

发布于 2024-09-11 14:46:05 字数 387 浏览 4 评论 0原文

我正在使用可区分联合构建表达式树。以下代码:

type IntExpression =
    | TrueIsOne of BoolExpression

type BoolExpression =
    | LessThan of IntExpression * IntExpression
    | And of BoolExpression * BoolExpression
    | Or of BoolExpression * BoolExpression
    | Bool of bool

抛出错误,因为未定义 BoolExpression。交换定义只会导致相反的结果(未定义 IntExpression),正如您所期望的那样。

有办法解决这个问题吗?

I'm building an expression tree using discriminated unions. The below code:

type IntExpression =
    | TrueIsOne of BoolExpression

type BoolExpression =
    | LessThan of IntExpression * IntExpression
    | And of BoolExpression * BoolExpression
    | Or of BoolExpression * BoolExpression
    | Bool of bool

throws an error because BoolExpression is not defined. Swapping the definitions just results in the reverse (IntExpression is not defined) as you would expect.

Is there a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-09-18 14:46:06

也许这会起作用:(

type IntExpression =
  ...
and BoolExpression = 
  ...

信息取自MSDN 上的此页面。 )

Perhaps this will work:

type IntExpression =
  ...
and BoolExpression = 
  ...

(Information taken from this page on MSDN.)

浮世清欢 2024-09-18 14:46:05

是的,使用 and 对具有相互依赖关系的类型定义进行分组:

type IntExpression =
    | TrueIsOne of BoolExpression

and BoolExpression =
    | LessThan of IntExpression * IntExpression
    | And of BoolExpression * BoolExpression
    | Or of BoolExpression * BoolExpression
    | Bool of bool

Yes, use and to group type definitions with inter-dependencies:

type IntExpression =
    | TrueIsOne of BoolExpression

and BoolExpression =
    | LessThan of IntExpression * IntExpression
    | And of BoolExpression * BoolExpression
    | Or of BoolExpression * BoolExpression
    | Bool of bool
迟月 2024-09-18 14:46:05

“and”通常适用于相互依赖的类型。也就是说,它适用于所有类型,例如 Mau 所示的可区分联合、类、记录和相互递归函数。

非终止示例:

let rec foo x = bar x
and bar x = foo x

"and" works generally for types with mutual dependencies. That is, it works for all types, such as discriminated unions, as shown by Mau, classes, records and mutually recursive functions.

Non terminating example:

let rec foo x = bar x
and bar x = foo x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文