-
Notifications
You must be signed in to change notification settings - Fork 29
/
AutoClose.scala
43 lines (34 loc) · 1.22 KB
/
AutoClose.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.{!!, Keyword}
import com.thoughtworks.dsl.keywords.Catch.CatchDsl
import scala.language.implicitConversions
import scala.util.Try
/**
* @author 杨博 (Yang Bo)
*/
final case class AutoClose[R <: AutoCloseable](open: () => R) extends AnyVal with Keyword[AutoClose[R], R]
/**
*
* @example
*/
object AutoClose {
implicit def implicitAutoClose[R <: AutoCloseable](r: => R): AutoClose[R] = AutoClose[R](r _)
def apply[R <: AutoCloseable](r: => R)(
implicit dummyImplicit: DummyImplicit = DummyImplicit.dummyImplicit): AutoClose[R] = new AutoClose(r _)
implicit def throwableContinuationAutoCloseDsl[Domain, Value, R <: AutoCloseable](
implicit catchDsl: CatchDsl[Domain, Domain, Value],
shiftDsl: Dsl[Shift[Domain, Value], Domain, Value]
): Dsl[AutoClose[R], Domain !! Value, R] =
new Dsl[AutoClose[R], Domain !! Value, R] {
def cpsApply(keyword: AutoClose[R], handler: R => Domain !! Value): Domain !! Value = _ {
val r = keyword.open()
val scopeResult = try {
!Shift(handler(r))
} finally {
r.close()
}
scopeResult
}
}
}