Reputation: 9469
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?
Upvotes: 14
Views: 812
Reputation: 5057
"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
Upvotes: 9
Reputation: 14468
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
Upvotes: 23
Reputation: 84755
Perhaps this will work:
type IntExpression =
...
and BoolExpression =
...
(Information taken from this page on MSDN.)
Upvotes: 4