如何在 Chapel 中产生一个 nilable 共享对象?

How to yield a nilable shared object in Chapel?

目前,我正在处理 Chapel 数据对象,但遇到了一些问题。我有一个名为 Row 的 class,它可以是 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();
      }
    }

如您所见,在一种情况下我 return 借用对象,而在其他情况下我 return 共享。有什么方法可以让我只 return 一种类型,无论是共享的还是借用的,因为目前在这种情况下我会出错。

        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();
  }
}