Chapel 中的静态 class 字段

Static class fields in Chapel

Chapel 是否具有 C++/Java 静态 class 字段的等效项,其值在 class 个对象之间共享?如果没有,应该怎么办?

Chapel 不支持等效的静态 class 字段。但是,它确实支持等效的静态 class 方法,在 Chapel 中称为 type methods

如果需要 Chapel 中的静态 class 字段,他们可以使用一种无​​括号的方法返回一个全局定义的变量以获得类似的效果,例如

var globalValue = 42;

class C {
  var a = 1;

  proc b {
    return globalValue;
  }
}

var c1 = new owned C(1);
var c2 = new owned C(2);

writeln(c1.a);
writeln(c1.b);

writeln(c2.a);
writeln(c2.b);

globalValue += 1;

writeln(c1.a);
writeln(c1.b);

writeln(c2.a);
writeln(c2.b);

输出:

1
42
2
42
1
43
2
43