Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using(new Source(...)) for Scala 2.11 #582

Closed
kwalcock opened this issue Feb 17, 2023 · 4 comments
Closed

Using(new Source(...)) for Scala 2.11 #582

kwalcock opened this issue Feb 17, 2023 · 4 comments
Assignees

Comments

@kwalcock
Copy link
Contributor

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.

@SethTisue
Copy link
Member

sounds like something we'd plausibly merge. want to take a shot at a PR?

@kwalcock
Copy link
Contributor Author

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()
}
}
so that it looks like that below.

  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.
    }
  }

@SethTisue
Copy link
Member

SethTisue commented Mar 24, 2023

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?

@kwalcock
Copy link
Contributor Author

See #587. It's more code duplication than I'd like to have, but I know of no better way. Code is copied and pasted between scala-2.11 and scala-2.12.

@SethTisue SethTisue self-assigned this Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants