memory-managementobject-lifetimechapeldata-objects

How to yield a nilable shared object in Chapel?


Currently, I am working on Chapel Data Objects and I am facing some issues. I have a class named Row which can be nil. I have these three methods:

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

As you can see that in one case I am returning a borrowed object while in other cases I am returning shared. Is there any way such that I only return one type, either shared or borrowed as currently in situations like this I am getting error.

        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"] );
        }

Solution

  • The idea is to typecast.

    override iter fetchall(): shared Row {
      var res: shared Row? = this.fetchone();
      while(res != nil) {
        try! {
          yield res: shared Row;
        }
        res = this.fetchone();
      }
    }