如何在 Chapel 中使用 `writeThis()` 结构
How to use the `writeThis()` construct in Chapel
我在 Chapel 中有一个 class,我想让它控制自己的打印语句。例如
class Noob {
var name: string, experience:int;
// constructor
}
之后,我想做一些类似的事情
me = new Noob('brian', 0)
writeln(me)
> Hi, my name is Brian and I have 0 years experience
看起来 this 应该有所帮助,但我无法完全使用 readThis()
方法。
您需要一个 writeThis(f)
方法来覆盖对象的默认 writeln
行为:
class Noob {
var name: string, experience:int;
proc writeThis(f) {
f.writef('Hi, my name is %s and I have %t years experience',
this.name, this.experience);
}
}
var noob = new Noob('ben', 2);
writeln(noob);
您还可以在 readWriteThis(f)
方法中使用 <~>
运算符来处理对象的读写:
class Noob {
var name: string, experience:int;
proc readWriteThis(f) {
f <~> 'Hi, my name is %s and I have %t years experience'.format(this.name, this.experience);
}
}
我在 Chapel 中有一个 class,我想让它控制自己的打印语句。例如
class Noob {
var name: string, experience:int;
// constructor
}
之后,我想做一些类似的事情
me = new Noob('brian', 0)
writeln(me)
> Hi, my name is Brian and I have 0 years experience
看起来 this 应该有所帮助,但我无法完全使用 readThis()
方法。
您需要一个 writeThis(f)
方法来覆盖对象的默认 writeln
行为:
class Noob {
var name: string, experience:int;
proc writeThis(f) {
f.writef('Hi, my name is %s and I have %t years experience',
this.name, this.experience);
}
}
var noob = new Noob('ben', 2);
writeln(noob);
您还可以在 readWriteThis(f)
方法中使用 <~>
运算符来处理对象的读写:
class Noob {
var name: string, experience:int;
proc readWriteThis(f) {
f <~> 'Hi, my name is %s and I have %t years experience'.format(this.name, this.experience);
}
}