【发布时间】:2013-08-06 20:47:39
【问题描述】:
说在标准 ML 中定义了几种数据类型:
datatype color = orange | navy | teal | silver | hsl of real * real * real;
datatype direction = east | north | west | south;
然后我定义一个使用上述数据类型的值:
type Cursor = int * int * color * direction;
val cursor : Cursor = (0, 0, orange, east);
它是一个“光标”,具有位置、绘图颜色和方向。 我希望它的属性最初是未定义的。我可以为每个 color 和 direction 数据类型添加一个构造函数(我将如何使用 int 位置值来做到这一点?),如下所示:
datatype color = orange | teal | silver | hsl of real * real * real | undefined;
datatype direction = east | north | west | south | undefined;
val cursor : Cursor = (0, 0, undefined, undefined);
我想这样做无需为每个数据类型显式定义一个额外的undefined 构造函数。你能想出一个好的、干净的解决方案吗?某种形式的泛型,无论类型如何,我都可以简单地使用“未定义值”的形式。粗略地说,类似于 Javas Nullable。
我的动机是我的光标的属性最初是未定义的。
【问题讨论】: