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 @   zjh6  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示