Chapel:了解托管 类 与 zip 和用户定义的迭代器的生命周期
Chapel : Understanding lifetime of managed classes with zip and user-defined iterators
我试图了解 owned
class 在用户定义的迭代器中使用时的生命周期。考虑以下代码:
var a = new owned C();
var b = new owned C();
a.i = 2;
forall (a1,b1) in zip(a,b) {
b1 = a1;
}
forall (a1,b1) in zip(a,b) {
writeln(a1, " ",b1);
}
class C {
var i : int;
iter these() {
yield 1;
}
iter these(param tag : iterKind) where tag==iterKind.leader {
yield 1;
}
iter these(param tag : iterKind, followThis) ref
where tag==iterKind.follower {
yield i;
}
}
编译和运行此代码出现以下错误
(08:54)$ chpl test.chpl --warn-unstable
(08:54)$ ./test
test.chpl:25: error: attempt to dereference nil
(08:54)$ chpl --version
chpl version 1.19.0 pre-release (2c10dbe)
我不清楚 class 何时在这里被 deinit
编辑。如果我将 owned
替换为 shared
,此示例将按预期工作。更有趣的是,将第一个循环更改为
forall (a1,b1) in zip(a.borrow(),b.borrow()) {
允许代码也能正常工作。在什么情况下参数会自动强制转换为 borrowed
实例?
我很确定这是一个错误。我去看看
In what cases is an argument automatically coerced into a borrowed instance?
现在,什么时候:
- 在其上调用方法时(
this
参数将借用)
- 当传递给类型为
C
或 borrowed C
的参数时(意思相同)。
- 传递给完全通用的函数参数时。
我不确定我们是否要遵守规则 3。但这不是你的问题 - 而问题是一些编译器引入的代码实现 forall 语句带走了 owned
值。这是一个错误。
我试图了解 owned
class 在用户定义的迭代器中使用时的生命周期。考虑以下代码:
var a = new owned C();
var b = new owned C();
a.i = 2;
forall (a1,b1) in zip(a,b) {
b1 = a1;
}
forall (a1,b1) in zip(a,b) {
writeln(a1, " ",b1);
}
class C {
var i : int;
iter these() {
yield 1;
}
iter these(param tag : iterKind) where tag==iterKind.leader {
yield 1;
}
iter these(param tag : iterKind, followThis) ref
where tag==iterKind.follower {
yield i;
}
}
编译和运行此代码出现以下错误
(08:54)$ chpl test.chpl --warn-unstable
(08:54)$ ./test
test.chpl:25: error: attempt to dereference nil
(08:54)$ chpl --version
chpl version 1.19.0 pre-release (2c10dbe)
我不清楚 class 何时在这里被 deinit
编辑。如果我将 owned
替换为 shared
,此示例将按预期工作。更有趣的是,将第一个循环更改为
forall (a1,b1) in zip(a.borrow(),b.borrow()) {
允许代码也能正常工作。在什么情况下参数会自动强制转换为 borrowed
实例?
我很确定这是一个错误。我去看看
In what cases is an argument automatically coerced into a borrowed instance?
现在,什么时候:
- 在其上调用方法时(
this
参数将借用) - 当传递给类型为
C
或borrowed C
的参数时(意思相同)。 - 传递给完全通用的函数参数时。
我不确定我们是否要遵守规则 3。但这不是你的问题 - 而问题是一些编译器引入的代码实现 forall 语句带走了 owned
值。这是一个错误。