Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use Using.Manager with Scala 3 given #18318

AugustNagro started this conversation in General Discussion
Discussion options

In Scala 2, it's common to write

import scala.util.Using
Using.Manager { implicit use =>
 runProgram()
}
def runProgram()(implicit use: Using.Manager): Unit =
 println("hello world")

How can we convert this to Scala 3?

You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

Can you provide a complete piece of code that compiles in Scala 2?

You must be logged in to vote
1 reply
Comment options

Sorry, edited :)

Comment options

inline def managed[T](inline op: Using.Manager ?=> T): Try[T] =
 Using.Manager(op(using _))
managed:
 runProgram()
You must be logged in to vote
3 replies
Comment options

That's great, although it would be ideal if users could write something like

Using.Manager { given use =>
 runProgram()
}

Directly. Or maybe def managed could be added to the StdLib eventually.

Comment options

Using.Manager { case given Using.Manager => runProgram() }

Comment options

@som-snytt very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /