Skip to content

Commit

Permalink
Merge pull request #255 from Atry/pr
Browse files Browse the repository at this point in the history
Add the Using.defer method
  • Loading branch information
Atry committed Jun 4, 2019
2 parents 90969cf + a75e4fe commit 07ec26b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
12 changes: 12 additions & 0 deletions keywords-Using/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.6-SNAP2" % Test

enablePlugins(Example)

import meta._
exampleSuperTypes := exampleSuperTypes.value.map {
case ctor"_root_.org.scalatest.FreeSpec" =>
ctor"_root_.org.scalatest.AsyncFreeSpec"
case otherTrait =>
otherTrait
}

scalacOptions ++= {
import Ordering.Implicits._
if (VersionNumber(scalaVersion.value).numbers < Seq(2L, 12L)) {
Expand All @@ -9,3 +19,5 @@ scalacOptions ++= {
None
}
}

libraryDependencies += "com.thoughtworks.dsl" %%% "keywords-await" % "1.3.0" % Test
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.thoughtworks.dsl.keywords
package com.thoughtworks.dsl
package keywords

import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.{!!, Keyword}
import com.thoughtworks.dsl.keywords.Catch.{CatchDsl, DslCatch}

import scala.concurrent.{ExecutionContext, Future}
import scala.language.implicitConversions
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

/** This [[Using]] keyword automatically manage resources in [[scala.concurrent.Future]], [[domains.task.Task]],
Expand All @@ -21,6 +21,44 @@ object Using {

implicit def implicitUsing[R <: AutoCloseable](r: => R): Using[R] = Using[R](r _)

trait ScopeExitHandler extends AutoCloseable

/** Returns a [[Using]] keyword to execute a [[ScopeExitHandler]] when exiting the nearest enclosing scope
* that is annotated as [[Dsl.reset @reset]],
* (or the nearest enclosing function if [[compilerplugins.ResetEverywhere]] is enabled).
*
* @note This method is similar to [[apply]],
* except the parameter type is changed from a generic `R` to the SAM type [[ScopeExitHandler]],
* which allows for function literal expressions
* in Scala 2.12+ or Scala 2.11 with `-Xexperimental` compiler option.
*
* @example The following function will perform `n *= 2` after `n += 20`:
*
* {{{
* import scala.concurrent.Future
* import com.thoughtworks.dsl.keywords.Using.scopeExit
* var n = 1
* def multiplicationAfterAddition = Future {
* !scopeExit { () =>
* n *= 2
* }
* n += 20
* }
* }}}
*
* Therefore, the final value of `n` should be `(1 + 20) * 2 = 42`.
*
* {{{
* import com.thoughtworks.dsl.Dsl.reset
* Future {
* !Await(multiplicationAfterAddition)
* n should be(42)
* }: @reset
* }}}
*
*/
def scopeExit(r: => ScopeExitHandler) = new Using(r _)

def apply[R <: AutoCloseable](r: => R)(
implicit dummyImplicit: DummyImplicit = DummyImplicit.dummyImplicit): Using[R] = new Using(r _)

Expand Down

0 comments on commit 07ec26b

Please sign in to comment.