如何从Discriminated Union案例中为联合案例分配类型?
码:
type ValidationResult<'Result> =
| Success of 'Result
| Failure of 'Result
type ValidationError =
| Error1 of Failure
| Error2 of Failure
Run Code Online (Sandbox Code Playgroud)
错误:
"失败"类型未定义
你不能这样做.受歧视的联合案例本身并不是类型 - 将它们视为返回DU类型值的构造函数.所以你的情况都Success和Failure一些方法来创建ValidationResult<'a>.
因此你需要做这样的事情,显然没有多大意义:
type ValidationError<'a> =
| Error1 of ValidationResult<'a>
| Error2 of ValidationResult<'a>
Run Code Online (Sandbox Code Playgroud)
这可能更接近你想要做的事情:
type ValidationError =
| Error1
| Error2
type ValidationResult<'Result> =
| Success of 'Result
| Failure of 'Result * ValidationError
Run Code Online (Sandbox Code Playgroud)