我希望某个事物(人、物品等)具有一些能力(跳跃、奔跑等)。我希望某些事物只具有特定的能力。以下是目前的类型:
type Ability =
| Jump
| Stay
| Run
| Walk
type Person = {
abilities : Ability Set // OK, since a person should be able to do all of the above
}
type InanimateObject = {
abilities : Ability Set // Not OK, it should only be able to "Stay"
}
type ThingWithAbilities =
| Person of Person
| InanimateObject of InanimateObject
我希望我的API调用者能够请求具有特定能力的ThingWithAbilities
。例如:给我所有具有“跳跃”能力的ThingWithAbilities
。如何以最佳方式建模此功能?我希望在代码中无法创建具有“跳跃”能力的InanimateObject
。
type Jump = Jump
是一个复杂的对象,其中名称Jump
指的是 DU,也指的是此 DU 的唯一情况。这可以简化为type Jump = class end
。 - Charles Roddie