结构构造函数

有时,您想为结构定义额外的构造函数,可以使用 ctor 功能。

struct Point {
  x: int;
  y: int;
}
ctor Point (a:int) => Point (a,a);
var p = Point 42;

事实上,这适用于任何类型,而不仅仅是一个结构,只要类型只有一个单词名。如果没有,你可以介绍一个 typedef

typedef pair = int * int;
ctor pair (x:int) => x,x;

使用A typedef 是为具有相同签名的类型引入额外构造函数的方法;即,提供 命名构造函数 . 例如:

struct complex {
  x: double;
  y: double;
}

typedef polar = complex;
ctor polar (modulus: double, argument: double) =>
  complex (modulus * cos argument, modulus * sin argument)
;