有没有办法将多个变体组合成一个?像这样的东西:
type pet = Cat | Dog;
type wild_animal = Deer | Lion;
type animal = pet | wild_animal;
这是一个语法错误,但我希望动物成为具有四个构造函数的变体:
Cat | Dog | Deer | Lion
。有没有办法做到这一点?
最佳答案
多态变体是根据您的想法创建的。它们作为内存表示的效率较低,但如果您要将其编译为 JavaScript,则无关紧要:
type pet = [ | `Cat | `Dog];
type wild_animal = [ | `Deer | `Lion];
type animal = [ pet | wild_animal ];
关于types - 将多个变体合并为一个变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50781063/