Skip to content

Commit a132b69

Browse files
committed
Use lambda expression for SAM type classes
1 parent f5b0334 commit a132b69

File tree

4 files changed

+164
-183
lines changed

4 files changed

+164
-183
lines changed

Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala

+146-172
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ object Dsl extends LowPriorityDsl0 {
277277
* !-notation is allowed by default for `? !! Throwable` and [[scala.concurrent.Future Future]] domains,
278278
* with the help of this type class.
279279
*/
280-
@implicitNotFound("The `try` ... `catch` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
280+
@implicitNotFound(
281+
"The `try` ... `catch` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
281282
trait TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] {
282283
def tryCatchFinally(block: BlockDomain !! Value,
283284
catcher: Catcher[BlockDomain !! Value],
@@ -296,26 +297,22 @@ object Dsl extends LowPriorityDsl0 {
296297
typeClass.tryCatchFinally(block, catcher, finalizer, outerSuccessHandler)
297298
}
298299

299-
def cpsApply[Value, OuterDomain, BlockDomain, FinalizerDomain](
300-
ops: Ops[Value, OuterDomain, BlockDomain, FinalizerDomain]) = ops
301-
302300
implicit def fromTryCatchTryFinally[Value, OuterDomain, BlockDomain, FinalizerDomain](
303301
implicit tryFinally: TryFinally[Value, OuterDomain, BlockDomain, FinalizerDomain],
304302
tryCatch: TryCatch[Value, BlockDomain, BlockDomain]
305-
): TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] =
306-
new TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] {
307-
def tryCatchFinally(block: BlockDomain !! Value,
308-
catcher: Catcher[BlockDomain !! Value],
309-
finalizer: FinalizerDomain !! Unit,
310-
outerSuccessHandler: Value => OuterDomain): OuterDomain = {
311-
tryFinally.tryFinally({
312-
tryCatch.tryCatch(block, catcher, _)
313-
}, finalizer, outerSuccessHandler)
314-
}
315-
}
303+
): TryCatchFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] = {
304+
(block: BlockDomain !! Value,
305+
catcher: Catcher[BlockDomain !! Value],
306+
finalizer: FinalizerDomain !! Unit,
307+
outerSuccessHandler: Value => OuterDomain) =>
308+
tryFinally.tryFinally({
309+
tryCatch.tryCatch(block, catcher, _)
310+
}, finalizer, outerSuccessHandler)
311+
}
316312
}
317313

318-
@implicitNotFound("The `try` ... `catch` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
314+
@implicitNotFound(
315+
"The `try` ... `catch` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
319316
trait TryCatch[Value, OuterDomain, BlockDomain] {
320317
def tryCatch(block: BlockDomain !! Value,
321318
catcher: Catcher[BlockDomain !! Value],
@@ -325,20 +322,18 @@ object Dsl extends LowPriorityDsl0 {
325322
private[dsl] trait LowPriorityTryCatch {
326323
implicit def liftFunction1TryCatch[Value, OuterDomain, BlockDomain, State](
327324
implicit restTryCatch: TryCatch[Value, OuterDomain, BlockDomain])
328-
: TryCatch[Value, State => OuterDomain, State => BlockDomain] =
329-
new TryCatch[Value, State => OuterDomain, State => BlockDomain] {
330-
def tryCatch(block: (State => BlockDomain) !! Value,
331-
catcher: Catcher[(State => BlockDomain) !! Value],
332-
outerSuccessHandler: Value => State => OuterDomain): State => OuterDomain = { state =>
333-
def withState(blockContinuation: (State => BlockDomain) !! Value) = { blockHandler: (Value => BlockDomain) =>
334-
blockContinuation { value: Value => (state: State) =>
335-
blockHandler(value)
336-
}(state)
337-
}
338-
339-
restTryCatch.tryCatch(withState(block), catcher.andThen(withState _), outerSuccessHandler(_)(state))
325+
: TryCatch[Value, State => OuterDomain, State => BlockDomain] = {
326+
(block: (State => BlockDomain) !! Value,
327+
catcher: Catcher[(State => BlockDomain) !! Value],
328+
outerSuccessHandler: Value => State => OuterDomain) => (state: State) =>
329+
def withState(blockContinuation: (State => BlockDomain) !! Value) = { blockHandler: (Value => BlockDomain) =>
330+
blockContinuation { value: Value => (state: State) =>
331+
blockHandler(value)
332+
}(state)
340333
}
341-
}
334+
335+
restTryCatch.tryCatch(withState(block), catcher.andThen(withState _), outerSuccessHandler(_)(state))
336+
}
342337
}
343338

344339
object TryCatch extends LowPriorityTryCatch {
@@ -351,95 +346,87 @@ object Dsl extends LowPriorityDsl0 {
351346
}
352347
}
353348

354-
def cpsApply[Value, OuterDomain, BlockDomain](ops: Ops[Value, OuterDomain, BlockDomain]) = ops
355-
356349
implicit def futureTryCatch[BlockValue, OuterValue](
357-
implicit executionContext: ExecutionContext): TryCatch[BlockValue, Future[OuterValue], Future[BlockValue]] =
358-
new TryCatch[BlockValue, Future[OuterValue], Future[BlockValue]] {
359-
def tryCatch(block: Future[BlockValue] !! BlockValue,
360-
catcher: Catcher[Future[BlockValue] !! BlockValue],
361-
outerSuccessHandler: BlockValue => Future[OuterValue]): Future[OuterValue] = {
362-
catchNativeException(block)
363-
.recoverWith {
364-
case e: Throwable =>
365-
def recover(): Future[BlockValue] = {
366-
(try {
367-
catcher.lift(e)
368-
} catch {
369-
case NonFatal(extractorException) =>
370-
return Future.failed(extractorException)
371-
}) match {
372-
case None =>
373-
Future.failed(e)
374-
case Some(recovered) =>
375-
catchNativeException(recovered)
376-
}
350+
implicit executionContext: ExecutionContext): TryCatch[BlockValue, Future[OuterValue], Future[BlockValue]] = {
351+
(block: Future[BlockValue] !! BlockValue,
352+
catcher: Catcher[Future[BlockValue] !! BlockValue],
353+
outerSuccessHandler: BlockValue => Future[OuterValue]) =>
354+
catchNativeException(block)
355+
.recoverWith {
356+
case e: Throwable =>
357+
def recover(): Future[BlockValue] = {
358+
(try {
359+
catcher.lift(e)
360+
} catch {
361+
case NonFatal(extractorException) =>
362+
return Future.failed(extractorException)
363+
}) match {
364+
case None =>
365+
Future.failed(e)
366+
case Some(recovered) =>
367+
catchNativeException(recovered)
377368
}
378-
recover()
379-
}
380-
.flatMap(outerSuccessHandler)
381-
}
382-
}
369+
}
370+
recover()
371+
}
372+
.flatMap(outerSuccessHandler)
373+
}
383374

384375
implicit def throwableContinuationTryCatch[LeftDomain, Value]
385376
: TryCatch[Value, LeftDomain !! Throwable, LeftDomain !! Throwable] = {
386-
new TryCatch[Value, LeftDomain !! Throwable, LeftDomain !! Throwable] {
387-
def tryCatch(block: LeftDomain !! Throwable !! Value,
388-
catcher: Catcher[LeftDomain !! Throwable !! Value],
389-
outerSuccessHandler: Value => LeftDomain !! Throwable): LeftDomain !! Throwable = {
390-
outerFailureHandler =>
391-
def innerFailureHandler(e: Throwable): LeftDomain = {
392-
catcher.lift(e) match {
393-
case None =>
394-
outerFailureHandler(e)
395-
case Some(recovered) =>
396-
@inline
397-
def recoveredHandler(): LeftDomain = {
398-
locally {
399-
try {
400-
recovered(outerSuccessHandler)
401-
} catch {
402-
case NonFatal(nativeThrown) =>
403-
return outerFailureHandler(nativeThrown)
404-
}
405-
}(outerFailureHandler)
406-
}
407377

408-
recoveredHandler()
378+
(block: LeftDomain !! Throwable !! Value,
379+
catcher: Catcher[LeftDomain !! Throwable !! Value],
380+
outerSuccessHandler: Value => LeftDomain !! Throwable) => outerFailureHandler =>
381+
def innerFailureHandler(e: Throwable): LeftDomain = {
382+
catcher.lift(e) match {
383+
case None =>
384+
outerFailureHandler(e)
385+
case Some(recovered) =>
386+
@inline
387+
def recoveredHandler(): LeftDomain = {
388+
locally {
389+
try {
390+
recovered(outerSuccessHandler)
391+
} catch {
392+
case NonFatal(nativeThrown) =>
393+
return outerFailureHandler(nativeThrown)
394+
}
395+
}(outerFailureHandler)
409396
}
410-
}
411397

412-
def runBlock(): LeftDomain = {
413-
(try {
414-
block { a => hookedFailureHandler =>
415-
@inline
416-
def successHandler(): LeftDomain = {
417-
locally {
418-
try {
419-
outerSuccessHandler(a)
420-
} catch {
421-
case NonFatal(nativeThrown) =>
422-
return outerFailureHandler(nativeThrown)
423-
}
424-
}(outerFailureHandler)
398+
recoveredHandler()
399+
}
400+
}
401+
402+
def runBlock(): LeftDomain = {
403+
(try {
404+
block { a => hookedFailureHandler =>
405+
@inline
406+
def successHandler(): LeftDomain = {
407+
locally {
408+
try {
409+
outerSuccessHandler(a)
410+
} catch {
411+
case NonFatal(nativeThrown) =>
412+
return outerFailureHandler(nativeThrown)
425413
}
414+
}(outerFailureHandler)
415+
}
426416

427-
successHandler()
428-
}
429-
} catch {
430-
case NonFatal(e) =>
431-
return innerFailureHandler(e)
432-
})(innerFailureHandler)
417+
successHandler()
433418
}
434-
runBlock()
419+
} catch {
420+
case NonFatal(e) =>
421+
return innerFailureHandler(e)
422+
})(innerFailureHandler)
435423
}
436-
}
437-
424+
runBlock()
438425
}
439-
440426
}
441427

442-
@implicitNotFound("The `try` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
428+
@implicitNotFound(
429+
"The `try` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}.")
443430
trait TryFinally[Value, OuterDomain, BlockDomain, FinalizerDomain] {
444431
def tryFinally(block: BlockDomain !! Value,
445432
finalizer: FinalizerDomain !! Unit,
@@ -449,20 +436,18 @@ object Dsl extends LowPriorityDsl0 {
449436
private[dsl] trait LowPriorityTryFinally {
450437
implicit def liftFunction1TryCatch[Value, OuterDomain, BlockDomain, FinalizerDomain, State](
451438
implicit restTryFinally: TryFinally[Value, OuterDomain, BlockDomain, FinalizerDomain])
452-
: TryFinally[Value, State => OuterDomain, State => BlockDomain, State => FinalizerDomain] =
453-
new TryFinally[Value, State => OuterDomain, State => BlockDomain, State => FinalizerDomain] {
454-
def tryFinally(block: (State => BlockDomain) !! Value,
455-
finalizer: (State => FinalizerDomain) !! Unit,
456-
outerSuccessHandler: Value => State => OuterDomain): State => OuterDomain = { state =>
457-
def withState[Domain, Value](blockContinuation: (State => Domain) !! Value) = { blockHandler: (Value => Domain) =>
458-
blockContinuation { value: Value => (state: State) =>
459-
blockHandler(value)
460-
}(state)
461-
}
462-
463-
restTryFinally.tryFinally(withState(block), withState(finalizer), outerSuccessHandler(_)(state))
439+
: TryFinally[Value, State => OuterDomain, State => BlockDomain, State => FinalizerDomain] = {
440+
(block: (State => BlockDomain) !! Value,
441+
finalizer: (State => FinalizerDomain) !! Unit,
442+
outerSuccessHandler: Value => State => OuterDomain) => state =>
443+
def withState[Domain, Value](blockContinuation: (State => Domain) !! Value) = { blockHandler: (Value => Domain) =>
444+
blockContinuation { value: Value => (state: State) =>
445+
blockHandler(value)
446+
}(state)
464447
}
465-
}
448+
449+
restTryFinally.tryFinally(withState(block), withState(finalizer), outerSuccessHandler(_)(state))
450+
}
466451
}
467452

468453
object TryFinally extends LowPriorityTryFinally {
@@ -476,75 +461,64 @@ object Dsl extends LowPriorityDsl0 {
476461
}
477462
}
478463

479-
def cpsApply[Value, OuterDomain, BlockDomain, FinalizerDomain](
480-
ops: Ops[Value, OuterDomain, BlockDomain, FinalizerDomain]) = ops
481-
482464
implicit def futureTryFinally[BlockValue, OuterValue](implicit executionContext: ExecutionContext)
483-
: TryFinally[BlockValue, Future[OuterValue], Future[BlockValue], Future[Unit]] =
484-
new TryFinally[BlockValue, Future[OuterValue], Future[BlockValue], Future[Unit]] {
485-
def tryFinally(block: Future[BlockValue] !! BlockValue,
486-
finalizer: Future[Unit] !! Unit,
487-
outerSuccessHandler: BlockValue => Future[OuterValue]): Future[OuterValue] = {
488-
@inline
489-
def injectFinalizer[A](f: Unit => Future[A]): Future[A] = {
490-
catchNativeException(finalizer).flatMap(f)
491-
}
492-
catchNativeException(block)
493-
.recoverWith {
494-
case e: Throwable =>
495-
injectFinalizer { _: Unit =>
496-
Future.failed(e)
497-
}
498-
}
499-
.flatMap { a =>
465+
: TryFinally[BlockValue, Future[OuterValue], Future[BlockValue], Future[Unit]] = {
466+
(block: Future[BlockValue] !! BlockValue,
467+
finalizer: Future[Unit] !! Unit,
468+
outerSuccessHandler: BlockValue => Future[OuterValue]) =>
469+
@inline
470+
def injectFinalizer[A](f: Unit => Future[A]): Future[A] = {
471+
catchNativeException(finalizer).flatMap(f)
472+
}
473+
474+
catchNativeException(block)
475+
.recoverWith {
476+
case e: Throwable =>
500477
injectFinalizer { _: Unit =>
501-
outerSuccessHandler(a)
478+
Future.failed(e)
502479
}
480+
}
481+
.flatMap { a =>
482+
injectFinalizer { _: Unit =>
483+
outerSuccessHandler(a)
503484
}
504-
}
505-
}
485+
}
486+
}
506487

507488
implicit def throwableContinuationTryFinally[LeftDomain, Value]
508489
: TryFinally[Value, LeftDomain !! Throwable, LeftDomain !! Throwable, LeftDomain !! Throwable] = {
509-
new TryFinally[Value, LeftDomain !! Throwable, LeftDomain !! Throwable, LeftDomain !! Throwable] {
510-
def tryFinally(block: LeftDomain !! Throwable !! Value,
511-
finalizer: LeftDomain !! Throwable !! Unit,
512-
outerSuccessHandler: Value => LeftDomain !! Throwable): LeftDomain !! Throwable = {
513-
outerFailureHandler =>
514-
@inline
515-
def injectFinalizer(finalizerHandler: Unit => LeftDomain !! Throwable): LeftDomain = {
516-
locally {
517-
try {
518-
finalizer(finalizerHandler)
519-
} catch {
520-
case NonFatal(e) =>
521-
return outerFailureHandler(e)
522-
}
523-
}(outerFailureHandler)
490+
(block, finalizer, outerSuccessHandler) => outerFailureHandler =>
491+
@inline
492+
def injectFinalizer(finalizerHandler: Unit => LeftDomain !! Throwable): LeftDomain = {
493+
locally {
494+
try {
495+
finalizer(finalizerHandler)
496+
} catch {
497+
case NonFatal(e) =>
498+
return outerFailureHandler(e)
524499
}
500+
}(outerFailureHandler)
501+
}
502+
503+
@inline
504+
def hookedFailureHandler(e: Throwable) =
505+
injectFinalizer { _: Unit =>
506+
_(e)
507+
}
525508

526-
@inline
527-
def hookedFailureHandler(e: Throwable) =
509+
def runBlock(): LeftDomain = {
510+
(try {
511+
block { value => hookedFailureHandler =>
528512
injectFinalizer { _: Unit =>
529-
_(e)
513+
outerSuccessHandler(value)
530514
}
531-
532-
def runBlock(): LeftDomain = {
533-
(try {
534-
block { value => hookedFailureHandler =>
535-
injectFinalizer { _: Unit =>
536-
outerSuccessHandler(value)
537-
}
538-
}
539-
} catch {
540-
case NonFatal(e) =>
541-
return hookedFailureHandler(e)
542-
})(hookedFailureHandler)
543515
}
544-
runBlock()
516+
} catch {
517+
case NonFatal(e) =>
518+
return hookedFailureHandler(e)
519+
})(hookedFailureHandler)
545520
}
546-
}
521+
runBlock()
547522
}
548523
}
549-
550524
}

0 commit comments

Comments
 (0)