-
Notifications
You must be signed in to change notification settings - Fork 29
/
Using.scala
43 lines (34 loc) · 1.18 KB
/
Using.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 Using[R <: AutoCloseable](open: () => R) extends AnyVal with Keyword[Using[R], R]
/**
*
* @example
*/
object Using {
implicit def implicitAutoClose[R <: AutoCloseable](r: => R): Using[R] = Using[R](r _)
def apply[R <: AutoCloseable](r: => R)(
implicit dummyImplicit: DummyImplicit = DummyImplicit.dummyImplicit): Using[R] = new Using(r _)
implicit def throwableContinuationAutoCloseDsl[Domain, Value, R <: AutoCloseable](
implicit catchDsl: CatchDsl[Domain, Domain, Value],
shiftDsl: Dsl[Shift[Domain, Value], Domain, Value]
): Dsl[Using[R], Domain !! Value, R] =
new Dsl[Using[R], Domain !! Value, R] {
def cpsApply(keyword: Using[R], handler: R => Domain !! Value): Domain !! Value = _ {
val r = keyword.open()
val scopeResult = try {
!Shift(handler(r))
} finally {
r.close()
}
scopeResult
}
}
}