gpt4 book ai didi

file - "channel"类型的字段给出错误 "Cannot default-initialize a variable with generic type"

转载 作者:行者123 更新时间:2023-12-01 01:15:05 25 4
gpt4 key购买 nike

在这段代码中,我创建了一个包含 channel 的类,该 channel 在初始化时可以根据请求打开。为此,我传递了一个标志来指示 channel 是否已打开(连同其文件名)。

class Myclass
{
var writeflag : bool;
var output : channel;

proc init( writeflag = false, filename = "" )
{
this.writeflag = writeflag;
if writeflag { // (1)
assert( filename != "" );
this.output = openwriter( filename );
} // (2)
}
}

proc main()
{
var a = new owned Myclass( writeflag = true,
filename = "test.out" );
a.output.writeln( 123 );
}

但是,编译器拒绝上述代码并显示以下消息:

myclass.chpl:6: error: Cannot default-initialize a variable with generic type myclass.chpl:6: note: 'not-fully-instantiated' has generic type 'channel'



另一方面,如果我注释掉 (1) 和 (2),代码会按预期工作并创建“test.out”。我猜这个问题与 channel 的通用性质有关。 (根据错误消息),但不太确定如何适本地编写这样的代码......

最佳答案

I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...



你在正确的轨道上。 channel record有一些必须在编译时提供的参数字段。您可以在字段声明中指定这些参数:
var output : channel(writing=true, kind=iokind.dynamic, locking=false);

关于file - "channel"类型的字段给出错误 "Cannot default-initialize a variable with generic type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com