原文 出现在自定义
动态数组类型
中.
//用-preview=dip1000编译
@safe:
struct Arr
{
int** ptr;
ref int* index() return scope {
return *ptr;
}
void assign(int* p) scope {
*ptr = p;
}
}
void main()
{
scope Arr a;
a.assign(a.index());
}
因为它按ref
返回域数组
元素,index
是return scope
.但是,把它传递给assign
函数时,会传递ref return
的副本
,隐式解引用
它,这样不再是域
.但是,编译器仍然会报错误:
a
域变量赋值给非域
参数.
问题在这:
ref int* index() return scope {
return *ptr;
}
应用scope
至this
引用,在本例中为ptr
.尽管*ptr
不是scope
,因为scope
不是传递的,因为给它讲了,编译器转移scope
到返回值.(这是一项功能
,而不是错误.)
基本
问题是代码使域有传递性
,但不行.有时,代码必须用@trusted
代码才能使其工作.
不,"中域
"注解
是正确的.因为我按ref
取解引用地址,为返回它,*ptr
再次变为scope
.如果按仅域
标记index
,编译器会正确地抱怨return*ptr
:错误:可能
不会返回this
域变量.
不,我的数组
结构与动态数组
完全一样.它不是让域指针
作为数组元素
.