简体   繁体   中英

Scala wrap generic anonymous class

I'm trying to make this code nicer by using scala closures:

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

I subclassed SQLiteQueue and added an overload to the execute function:

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

so I can use it something like this:

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

But I get this compiler error at 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]]

the execute function I'm calling there looks like this: public <T, J extends SQLiteJob<T>> J execute(J job)

在调用execute时指定类型参数:

super.execute[T, SQLiteJob[T]](job)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM