Using(new Source(...)) for Scala 2.11
kwalcock opened this issue · 4 comments
In Scala 2.11, Source is not Closeable so that Using(new Source(...)) results in
[error] ...: could not find implicit value for parameter releasable: scala.util.Using.Releasable[scala.io.BufferedSource]
[error] Using.resource(Source.fromFile(...)) { source =>
[error] ^
[error] one error found
This can be worked around with code like
implicit object SourceReleaser extends Releasable[Source] {
override def release(resource: Source): Unit = resource.close
}If I add that somewhere, I believe that an additional import will be needed in my code. If it gets added somewhere inside scala.util.Using, which seems to be part of this project, the extra import might not be necessary and that would be very nice to have.
sounds like something we'd plausibly merge. want to take a shot at a PR?
Some code can be inserted at
object Releasable {
/** An implicit `Releasable` for [[java.lang.AutoCloseable `AutoCloseable`s]]. */
implicit object AutoCloseableIsReleasable extends Releasable[AutoCloseable] {
def release(resource: AutoCloseable): Unit = resource.close()
}
/** An implicit `Releasable` for [[scala.io.Source `Source`s]] which aren't [[java.lang.AutoCloseable `AutoCloseable`s]] in Scala 2.11. */
implicit object SourceReleasable extends Releasable[Source] {
def release(resource: Source): Unit = resource.close()
}
}However, that causes problems for Scala 2.12 because the conversion is then ambiguous. I do not know how to make the Scala 2.11 and 2.12 versions different without duplicating most of the code from the scala-2.11_2.12 directory and putting copies in scala-2.11 and scala-2.12. Other people probably do know how to do this.
My test case was
import scala.io.Source
...
@Test
def usingSource(): Unit = {
Using(Source.fromString("Hello, Source!")) { source =>
// If this simply compiles, then mission accomplished.
}
}I do not know how to make the Scala 2.11 and 2.12 versions different
I could easily be missing something, but we do have src/main/scala-2.11 and src/main/scala-2.12 directories for code that is specific to those versions, alongside the src/main/scala-2.11_2.12 directory for code that is shared by the two. So it isn't clear to me that any other code besides the directly affected code would need to be copy/pasted?