0

我试图通过使用 scala 闭包使这段代码更好:

  SQLiteQueue queue = new SQLiteQueue(databaseFile);
  queue.start();    
  queue.execute(new SQLiteJob<Object>() {
    protected Object job(SQLiteConnection connection) throws SQLiteException {
      connection.exec(...);
      return null;
    }
  });

我将 SQLiteQueue 子类化并为执行函数添加了一个重载:

def execute[T](action: SQLiteConnection => T) {
    val job = new SQLiteJob[T] {
        override def job(conn:SQLiteConnection):T = {
            action(conn)
        }
    }
    super.execute(job)
}

所以我可以像这样使用它:

queue.execute { conn => do something with conn}

但我得到这个编译器错误super.execute(job)

error: inferred type arguments [Nothing,com.almworks.sqlite4java.SQLiteJob[T]] 
do not conform to method execute's type parameter bounds [T,J <: 
com.almworks.sqlite4java.SQLiteJob[T]]

我在那里调用的执行函数如下所示:public <T, J extends SQLiteJob<T>> J execute(J job)

4

1 回答 1

3

调用execute时指定类型参数:

super.execute[T, SQLiteJob[T]](job)
于 2012-10-16T21:44:58.347 回答