d避免初化后修改字段

struct S {
  const int i;

  this(int i) {
    // 第1次赋值为初化
    this.i = i;
  }
}

void main() {
  auto s = S(42);

  // 不行
  s.i = 43;

  //也不行
  s = S(44);
}
posted @ 2021-08-08 20:54  zjh6  阅读(10)  评论(0)    收藏  举报  来源