我有一个像这样的简单类型:
/// <summary>
/// An attribute consists of a key and all possible values.
/// </summary>
type IAttribute<'a when 'a: comparison> =
abstract Key: string
abstract Values: seq<'a>
根据这个定义,我创建了这样的实现:
let numericAttribute values =
{ new IAttribute<float> with
member this.Key = "Numeric"
member this.Values = values }
let enumerationAttribute values =
{ new IAttribute<string> with
member this.Key = "Enumeration"
member this.Values = values }
例子:
let numAttr = numericAttribute [| 1.0; 4.0; 6.0; 20.0; 70.0 |]
let enAttr = enumerationAttribute [| "val1"; "val2"; "val3" |]
现在我可以创建实例:
let num1 = new AttributeInstance<float>(numAttr, 4.0)
let num2 = new AttributeInstance<float>(numAttr, 6.0)
let en1 = new AttributeInstance<string>(enAttr, "val1")
AttributeInstance 是一种类型,它只是特定属性类型的元组和与该属性类型兼容的值。
我想要一个简单的树:
type Tree<'a when 'a: comparison> =
| Leaf of 'a
| SubTree of AttributeInstance<'a> * seq<Tree<'a>>
我的问题是,在树的不同级别,我希望能够拥有不同的类型。在一个级别上,我希望拥有一个属性为 en1 的子树,而在下一个级别上,我希望能够拥有 num1(或 num2)。
有人可以帮我概括或重新考虑这一点吗?