Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 0 additions & 306 deletions Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -397,312 +397,6 @@ object Dsl extends LowPriorityDsl0 {

private[dsl] type !![R, +A] = (A => R) => R

private def catchNativeException[A](
futureContinuation: Future[A] !! A
): Future[A] = {
try {
futureContinuation(Future.successful)
} catch {
case NonFatal(e) =>
Future.failed(e)
}
}

/** The type class to support `try` ... `catch` ... `finally` expression for
* `OutputDomain`.
*
* !-notation is allowed by default for `? !! Throwable` and
* [[scala.concurrent.Future Future]] domains, with the help of this type
* class.
*/
@implicitNotFound(
"The `try` ... `catch` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
)
trait TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] {
def tryCatchFinally(
block: BlockDomain !! Value,
catcher: Catcher[BlockDomain !! Value],
finalizer: FinalizerDomain !! Unit,
outerSuccessHandler: Value => OuterDomain
): OuterDomain
}

object TryCatchFinally {

implicit def fromTryCatchTryFinally[
Value,
OuterDomain,
BlockDomain,
FinalizerDomain
](implicit
tryFinally: TryFinally[
Value,
OuterDomain,
BlockDomain,
FinalizerDomain
],
tryCatch: TryCatch[Value, BlockDomain, BlockDomain]
): TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] = {
(
block: BlockDomain !! Value,
catcher: Catcher[BlockDomain !! Value],
finalizer: FinalizerDomain !! Unit,
outerSuccessHandler: Value => OuterDomain
) =>
tryFinally.tryFinally(
{
tryCatch.tryCatch(block, catcher, _)
},
finalizer,
outerSuccessHandler
)
}
}

@implicitNotFound(
"The `try` ... `catch` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
)
trait TryCatch[Value, OuterDomain, BlockDomain] {
def tryCatch(
block: BlockDomain !! Value,
catcher: Catcher[BlockDomain !! Value],
outerSuccessHandler: Value => OuterDomain
): OuterDomain
}

private[dsl] trait LowPriorityTryCatch {
implicit def liftFunction1TryCatch[Value, OuterDomain, BlockDomain, State](
implicit restTryCatch: TryCatch[Value, OuterDomain, BlockDomain]
): TryCatch[Value, State => OuterDomain, State => BlockDomain] = {
(
block: (State => BlockDomain) !! Value,
catcher: Catcher[(State => BlockDomain) !! Value],
outerSuccessHandler: Value => State => OuterDomain
) => (state: State) =>
def withState(blockContinuation: (State => BlockDomain) !! Value) = {
(blockHandler: (Value => BlockDomain)) =>
blockContinuation { (value: Value) => (state: State) =>
blockHandler(value)
}(state)
}

restTryCatch.tryCatch(
withState(block),
catcher.andThen(withState _),
outerSuccessHandler(_)(state)
)
}
}

object TryCatch extends LowPriorityTryCatch {

implicit def throwableContinuationTryCatch[LeftDomain, Value]
: TryCatch[Value, LeftDomain !! Throwable, LeftDomain !! Throwable] = {

(
block: LeftDomain !! Throwable !! Value,
catcher: Catcher[LeftDomain !! Throwable !! Value],
outerSuccessHandler: Value => LeftDomain !! Throwable
) => outerFailureHandler =>
def innerFailureHandler(e: Throwable): LeftDomain = {
catcher.lift(e) match {
case None =>
outerFailureHandler(e)
case Some(recovered) =>
@inline
def recoveredHandler(): LeftDomain = {
locally {
try {
recovered(outerSuccessHandler)
} catch {
case NonFatal(nativeThrown) =>
return outerFailureHandler(nativeThrown)
}
}(outerFailureHandler)
}

recoveredHandler()
}
}

def runBlock(): LeftDomain = {
(try {
block { a => hookedFailureHandler =>
@inline
def successHandler(): LeftDomain = {
locally {
try {
outerSuccessHandler(a)
} catch {
case NonFatal(nativeThrown) =>
return outerFailureHandler(nativeThrown)
}
}(outerFailureHandler)
}

successHandler()
}
} catch {
case NonFatal(e) =>
return innerFailureHandler(e)
})(innerFailureHandler)
}
runBlock()
}

implicit def futureTryCatch[BlockValue, OuterValue](implicit
executionContext: ExecutionContext
): TryCatch[BlockValue, Future[OuterValue], Future[BlockValue]] = {
(
block: Future[BlockValue] !! BlockValue,
catcher: Catcher[Future[BlockValue] !! BlockValue],
outerSuccessHandler: BlockValue => Future[OuterValue]
) =>
catchNativeException(block)
.recoverWith { case e: Throwable =>
def recover(): Future[BlockValue] = {
(try {
catcher.lift(e)
} catch {
case NonFatal(extractorException) =>
return Future.failed(extractorException)
}) match {
case None =>
Future.failed(e)
case Some(recovered) =>
catchNativeException(recovered)
}
}
recover()
}
.flatMap(outerSuccessHandler)
}
}

@implicitNotFound(
"The `try` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
)
trait TryFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] {
def tryFinally(
block: BlockDomain !! Value,
finalizer: FinalizerDomain !! Unit,
outerSuccessHandler: Value => OuterDomain
): OuterDomain
}

private[dsl] trait LowPriorityTryFinally {
implicit def liftFunction1TryCatch[
Value,
OuterDomain,
BlockDomain,
FinalizerDomain,
State
](implicit
restTryFinally: TryFinally[
Value,
OuterDomain,
BlockDomain,
FinalizerDomain
]
): TryFinally[
Value,
State => OuterDomain,
State => BlockDomain,
State => FinalizerDomain
] = {
(
block: (State => BlockDomain) !! Value,
finalizer: (State => FinalizerDomain) !! Unit,
outerSuccessHandler: Value => State => OuterDomain
) => state =>
def withState[Domain, Value](
blockContinuation: (State => Domain) !! Value
) = { (blockHandler: (Value => Domain)) =>
blockContinuation { (value: Value) => (state: State) =>
blockHandler(value)
}(state)
}

restTryFinally.tryFinally(
withState(block),
withState(finalizer),
outerSuccessHandler(_)(state)
)
}
}

object TryFinally extends LowPriorityTryFinally {

implicit def futureTryFinally[BlockValue, OuterValue](implicit
executionContext: ExecutionContext
): TryFinally[BlockValue, Future[OuterValue], Future[BlockValue], Future[
Unit
]] = {
(
block: Future[BlockValue] !! BlockValue,
finalizer: Future[Unit] !! Unit,
outerSuccessHandler: BlockValue => Future[OuterValue]
) =>
@inline
def injectFinalizer[A](f: Unit => Future[A]): Future[A] = {
catchNativeException(finalizer).flatMap(f)
}

catchNativeException(block)
.recoverWith { case e: Throwable =>
injectFinalizer { (_: Unit) =>
Future.failed(e)
}
}
.flatMap { a =>
injectFinalizer { (_: Unit) =>
outerSuccessHandler(a)
}
}
}

implicit def throwableContinuationTryFinally[LeftDomain, Value]: TryFinally[
Value,
LeftDomain !! Throwable,
LeftDomain !! Throwable,
LeftDomain !! Throwable
] = { (block, finalizer, outerSuccessHandler) => outerFailureHandler =>
@inline
def injectFinalizer(
finalizerHandler: Unit => LeftDomain !! Throwable
): LeftDomain = {
locally {
try {
finalizer(finalizerHandler)
} catch {
case NonFatal(e) =>
return outerFailureHandler(e)
}
}(outerFailureHandler)
}

@inline
def hookedFailureHandler(e: Throwable) =
injectFinalizer { (_: Unit) =>
_(e)
}

def runBlock(): LeftDomain = {
(try {
block { value => hookedFailureHandler =>
injectFinalizer { (_: Unit) =>
outerSuccessHandler(value)
}
}
} catch {
case NonFatal(e) =>
return hookedFailureHandler(e)
})(hookedFailureHandler)
}
runBlock()
}
}

@FunctionalInterface
trait Lift[From, +To] extends (From => To)
private[dsl] trait LowPriorityLift0 { this: Lift.type =>
Expand Down
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ lazy val `keywords-TryCatchFinally` =
lazy val `keywords-TryFinally` =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.dependsOn(Dsl, `keywords-Shift`, `keywords-Match`)
.dependsOn(Dsl, `keywords-TryCatch`)

lazy val `keywords-While` =
crossProject(JSPlatform, JVMPlatform)
Expand Down Expand Up @@ -186,6 +186,8 @@ lazy val `domains-scalaz` =
reset % Test,
`keywords-Monadic`,
`keywords-Return`,
`keywords-TryCatch`,
`keywords-TryFinally`,
`keywords-Shift` % Test,
`keywords-Yield` % Test
)
Expand Down
Loading