标签: heterogeneous-list

异构列表的元素是否可能取决于前面元素的类型?

语境

我想对 Web 应用程序的路由进行建模,使其满足以下要求:

  1. 可以强制执行完整的定义
  2. 可以创建不完整的定义
  3. 可以检查不完整的定义是否“匹配”(即包含在)完整的定义。

作为使用的示例:

type root =
  | Fruit of fruit
  | Veggie of veggie
  
and fruit =
  | Apple of apple
  | Banana of banana
  
and veggie =
  | Carrot of carrot
  
and apple = { diameter: int; cultivar: string; }
and banana = { length: int }
and carrot = { length: int; color: [`Orange | `Purple] }
Run Code Online (Sandbox Code Playgroud)

有了这个,我们可以轻松创建和执行完整的定义:

let complete = Fruit (Apple { diameter = 8; cultivar = "Golden Delicious" })
Run Code Online (Sandbox Code Playgroud)

但无法创建不完整的定义 …

ocaml gadt heterogeneous-list

5
推荐指数
1
解决办法
138
查看次数

标签 统计

gadt ×1

heterogeneous-list ×1

ocaml ×1