我正在尝试使用新的 Scala 2.10implicit class
机制将 a 转换java.sql.ResultSet
为scala.collection.immutable.Stream
. 在 Scala 2.9 中,我使用以下代码,该代码有效:
/**
* Implicitly convert a ResultSet to a Stream[ResultSet]. The Stream can then be
* traversed using the usual methods map, filter, etc.
*
* @param resultSet the Result to convert
* @return a Stream wrapped around the ResultSet
*/
implicit def resultSet2Stream(resultSet: ResultSet): Stream[ResultSet] = {
if (resultSet.next) Stream.cons(resultSet, resultSet2Stream(resultSet))
else {
resultSet.close()
Stream.empty
}
}
然后我可以像这样使用它:
val resultSet = statement.executeQuery("SELECT * FROM foo")
resultSet.map {
row => /* ... */
}
implicit class
我想出的看起来像这样:
/**
* Implicitly convert a ResultSet to a Stream[ResultSet]. The Stream can then be
* traversed using the usual map, filter, etc.
*/
implicit class ResultSetStream(val row: ResultSet)
extends AnyVal {
def toStream: Stream[ResultSet] = {
if (row.next) Stream.cons(row, row.toStream)
else {
row.close()
Stream.empty
}
}
}
但是,现在我必须调用toStream
,ResultSet
这会破坏“隐式”部分:
val resultSet = statement.executeQuery("SELECT * FROM foo")
resultSet.toStream.map {
row => /* ... */
}
我究竟做错了什么?
我还应该使用implicit def
andimport scala.language.implicitConversions
来避免“功能”警告吗?
更新
ResultSet
这是将 转换为scala.collection.Iterator
(仅 Scala 2.10+)的替代解决方案:
/*
* Treat a java.sql.ResultSet as an Iterator, allowing operations like filter,
* map, etc.
*
* Sample usage:
* val resultSet = statement.executeQuery("...")
* resultSet.map {
* resultSet =>
* // ...
* }
*/
implicit class ResultSetIterator(resultSet: ResultSet)
extends Iterator[ResultSet] {
def hasNext: Boolean = resultSet.next()
def next() = resultSet
}