假设我在 F# 中有一个这样的类型:
type public Expression =
| Identifier of string
| BooleanConstant of bool
| StringConstant of string
| IntegerConstant of int
| Vector of Expression list
// etc...
现在我想使用这种类型来构建 map :
definitions : Map<Identifier, Expression>
但是,这给出了错误:
The type 'identifier' is not defined
如何使用我的类型大小写作为类型参数?
最佳答案
Identifier
是一个 case 构造函数,而不是一个类型。它实际上是一个类型为 string -> Expression
的函数.案例类型为string
,因此您可以定义 definitions
作为
type definitions : Map<string, Expression>
关于generics - 如何在类型参数中使用可区分联合分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40502674/