目前,我正在研究Chapel数据对象,并面临一些问题。我有一个名为Row的类,它可以是nil。我有这三种方法:
override iter these()ref {
for row in this.fetchall(){
yield row;
}
}
override proc fetchone(): shared Row? {
if this.curRow == this.numRows {
return nil;
}
var row = new shared Row();
var rowNum: int(32) = 0;
mysql_data_seek(this.res, this.curRow: c_int);
var _row = mysql_fetch_row(this.res);
while(rowNum < this.nFields) {
var datum: string = __get_mysql_row_by_number(_row, rowNum: c_int);
var colinfo = this.getColumnInfo(rowNum);
row.addData(colinfo.name,datum);
rowNum += 1;
}
this.curRow += 1;
return row;
}
override iter fetchall(): borrowed Row {
var res: shared Row? = this.fetchone();
while(res != nil) {
yield res!;
res = this.fetchone();
}
}
正如您所看到的,在一种情况下,我返回借用的对象,而在其他情况下,返回共享对象。有没有什么方法可以让我只返回一个类型,无论是共享的还是借用的,因为目前在这种情况下我会出错。
var res: Row? = cursor.fetchone();
for row in cursor {
res = row;
//get row data by column name and print it.
writeln("name = ", row["name"]," email = ", row["email"] );
}
这个想法是进行类型转换。
override iter fetchall(): shared Row {
var res: shared Row? = this.fetchone();
while(res != nil) {
try! {
yield res: shared Row;
}
res = this.fetchone();
}
}