在案例类型签名中使用先前案例

时间:2016-05-11 19:10:09

标签: f# self-reference discriminated-union

我知道有歧视的工会可以自称,例如

<ObjectDataProvider x:Key="MyList" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="MyEnum" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

但有没有办法在类型签名中引用其他案例?以下两个都会引发“未定义类型'年'的错误”&amp; “类型'月'未定义”

type Tree=
    | Node of Tree
    | Leaf
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 )会允许这样的使用?

1 个答案:

答案 0 :(得分:4)

我认为你对工会案件的构成感到困惑。您不能将联合案例作为类型引用。我认为你要找的是这样的单例DUs:

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