Search code examples
f#self-referencediscriminated-union

Using prior cases in case type signature


I know that discriminated unions can refer to themselves, e.g.

type Tree=
    | Node of Tree
    | Leaf

but is there any means to refer to other cases in the type signatures? Both of the following raise errors that "The type 'Year' is not defined" & "The type 'Month' is not defined"

type Period =
    | Year of int
    | Month of Year * int
    | Day of Month * int
type Period' =
    | Year of int
    | Month of Period'.Year * int
    | Day of Period'.Month * int

Is there some form of annotation or a keyword I've yet to encounter (analogous to rec) that would permit such a usage?


Solution

  • I think you are confused about what constitutes a union case. You cannot reference a union case as a type. I think what you're looking for is single case DUs like this:

    type Year = Year of int
    type Month = | Month of Year * int
    type Day = Month * int
    type Period =
      | Year of Year
      | Month of Month
      | Day of Day