d无间接隐式转为可变2

原文
按值传递相当于浅(1级深)不等.遗憾的是,在当前语言中无法表示.

struct A {
    int[] slice;
}

void main() {
     const a = A([1, 2, 3]);
     pass(a);
     // 仍有常
}

void pass(Unqual!(const A) value) {
    // 值是:
    // struct A { const(int)[]slice; }
    value.slice ~= 4; // 应工作.
}

另外是auto是否去掉const/immutable.

const i = 4; // const int
auto v = i;  // int
const a = [0]; // const(int[])
auto s = a;    // const(int)[]
//
void main()
{
 const i = 4;  // const(int)
 auto v = i;  // const(int)
 /* onlineapp.d(6): Error: cannot modify `const` expression `v`
  --v; //*/
 const a = [0]; // const(const(int)[])
 auto b = a;  // const(const(int)[])

 const c= [i]; // const(const(int)[])
 auto d= c;   // const(const(int)[])
  //typeid(d).writeln;
}
posted @ 2022-09-05 11:17  zjh6  阅读(11)  评论(0)    收藏  举报  来源