@@ -397,312 +397,6 @@ object Dsl extends LowPriorityDsl0 {
397397
398398 private [dsl] type !! [R , + A ] = (A => R ) => R
399399
400- private def catchNativeException [A ](
401- futureContinuation : Future [A ] !! A
402- ): Future [A ] = {
403- try {
404- futureContinuation(Future .successful)
405- } catch {
406- case NonFatal (e) =>
407- Future .failed(e)
408- }
409- }
410-
411- /** The type class to support `try` ... `catch` ... `finally` expression for
412- * `OutputDomain`.
413- *
414- * !-notation is allowed by default for `? !! Throwable` and
415- * [[scala.concurrent.Future Future ]] domains, with the help of this type
416- * class.
417- */
418- @ implicitNotFound(
419- " The `try` ... `catch` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
420- )
421- trait TryCatchFinally [Value , OuterDomain , BlockDomain , FinalizerDomain ] {
422- def tryCatchFinally (
423- block : BlockDomain !! Value ,
424- catcher : Catcher [BlockDomain !! Value ],
425- finalizer : FinalizerDomain !! Unit ,
426- outerSuccessHandler : Value => OuterDomain
427- ): OuterDomain
428- }
429-
430- object TryCatchFinally {
431-
432- implicit def fromTryCatchTryFinally [
433- Value ,
434- OuterDomain ,
435- BlockDomain ,
436- FinalizerDomain
437- ](implicit
438- tryFinally : TryFinally [
439- Value ,
440- OuterDomain ,
441- BlockDomain ,
442- FinalizerDomain
443- ],
444- tryCatch : TryCatch [Value , BlockDomain , BlockDomain ]
445- ): TryCatchFinally [Value , OuterDomain , BlockDomain , FinalizerDomain ] = {
446- (
447- block : BlockDomain !! Value ,
448- catcher : Catcher [BlockDomain !! Value ],
449- finalizer : FinalizerDomain !! Unit ,
450- outerSuccessHandler : Value => OuterDomain
451- ) =>
452- tryFinally.tryFinally(
453- {
454- tryCatch.tryCatch(block, catcher, _)
455- },
456- finalizer,
457- outerSuccessHandler
458- )
459- }
460- }
461-
462- @ implicitNotFound(
463- " The `try` ... `catch` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
464- )
465- trait TryCatch [Value , OuterDomain , BlockDomain ] {
466- def tryCatch (
467- block : BlockDomain !! Value ,
468- catcher : Catcher [BlockDomain !! Value ],
469- outerSuccessHandler : Value => OuterDomain
470- ): OuterDomain
471- }
472-
473- private [dsl] trait LowPriorityTryCatch {
474- implicit def liftFunction1TryCatch [Value , OuterDomain , BlockDomain , State ](
475- implicit restTryCatch : TryCatch [Value , OuterDomain , BlockDomain ]
476- ): TryCatch [Value , State => OuterDomain , State => BlockDomain ] = {
477- (
478- block : (State => BlockDomain ) !! Value ,
479- catcher : Catcher [(State => BlockDomain ) !! Value ],
480- outerSuccessHandler : Value => State => OuterDomain
481- ) => (state : State ) =>
482- def withState (blockContinuation : (State => BlockDomain ) !! Value ) = {
483- (blockHandler : (Value => BlockDomain )) =>
484- blockContinuation { (value : Value ) => (state : State ) =>
485- blockHandler(value)
486- }(state)
487- }
488-
489- restTryCatch.tryCatch(
490- withState(block),
491- catcher.andThen(withState _),
492- outerSuccessHandler(_)(state)
493- )
494- }
495- }
496-
497- object TryCatch extends LowPriorityTryCatch {
498-
499- implicit def throwableContinuationTryCatch [LeftDomain , Value ]
500- : TryCatch [Value , LeftDomain !! Throwable , LeftDomain !! Throwable ] = {
501-
502- (
503- block : LeftDomain !! Throwable !! Value ,
504- catcher : Catcher [LeftDomain !! Throwable !! Value ],
505- outerSuccessHandler : Value => LeftDomain !! Throwable
506- ) => outerFailureHandler =>
507- def innerFailureHandler (e : Throwable ): LeftDomain = {
508- catcher.lift(e) match {
509- case None =>
510- outerFailureHandler(e)
511- case Some (recovered) =>
512- @ inline
513- def recoveredHandler (): LeftDomain = {
514- locally {
515- try {
516- recovered(outerSuccessHandler)
517- } catch {
518- case NonFatal (nativeThrown) =>
519- return outerFailureHandler(nativeThrown)
520- }
521- }(outerFailureHandler)
522- }
523-
524- recoveredHandler()
525- }
526- }
527-
528- def runBlock (): LeftDomain = {
529- (try {
530- block { a => hookedFailureHandler =>
531- @ inline
532- def successHandler (): LeftDomain = {
533- locally {
534- try {
535- outerSuccessHandler(a)
536- } catch {
537- case NonFatal (nativeThrown) =>
538- return outerFailureHandler(nativeThrown)
539- }
540- }(outerFailureHandler)
541- }
542-
543- successHandler()
544- }
545- } catch {
546- case NonFatal (e) =>
547- return innerFailureHandler(e)
548- })(innerFailureHandler)
549- }
550- runBlock()
551- }
552-
553- implicit def futureTryCatch [BlockValue , OuterValue ](implicit
554- executionContext : ExecutionContext
555- ): TryCatch [BlockValue , Future [OuterValue ], Future [BlockValue ]] = {
556- (
557- block : Future [BlockValue ] !! BlockValue ,
558- catcher : Catcher [Future [BlockValue ] !! BlockValue ],
559- outerSuccessHandler : BlockValue => Future [OuterValue ]
560- ) =>
561- catchNativeException(block)
562- .recoverWith { case e : Throwable =>
563- def recover (): Future [BlockValue ] = {
564- (try {
565- catcher.lift(e)
566- } catch {
567- case NonFatal (extractorException) =>
568- return Future .failed(extractorException)
569- }) match {
570- case None =>
571- Future .failed(e)
572- case Some (recovered) =>
573- catchNativeException(recovered)
574- }
575- }
576- recover()
577- }
578- .flatMap(outerSuccessHandler)
579- }
580- }
581-
582- @ implicitNotFound(
583- " The `try` ... `finally` expression cannot contain !-notation inside a function that returns ${OuterDomain}."
584- )
585- trait TryFinally [Value , OuterDomain , BlockDomain , FinalizerDomain ] {
586- def tryFinally (
587- block : BlockDomain !! Value ,
588- finalizer : FinalizerDomain !! Unit ,
589- outerSuccessHandler : Value => OuterDomain
590- ): OuterDomain
591- }
592-
593- private [dsl] trait LowPriorityTryFinally {
594- implicit def liftFunction1TryCatch [
595- Value ,
596- OuterDomain ,
597- BlockDomain ,
598- FinalizerDomain ,
599- State
600- ](implicit
601- restTryFinally : TryFinally [
602- Value ,
603- OuterDomain ,
604- BlockDomain ,
605- FinalizerDomain
606- ]
607- ): TryFinally [
608- Value ,
609- State => OuterDomain ,
610- State => BlockDomain ,
611- State => FinalizerDomain
612- ] = {
613- (
614- block : (State => BlockDomain ) !! Value ,
615- finalizer : (State => FinalizerDomain ) !! Unit ,
616- outerSuccessHandler : Value => State => OuterDomain
617- ) => state =>
618- def withState [Domain , Value ](
619- blockContinuation : (State => Domain ) !! Value
620- ) = { (blockHandler : (Value => Domain )) =>
621- blockContinuation { (value : Value ) => (state : State ) =>
622- blockHandler(value)
623- }(state)
624- }
625-
626- restTryFinally.tryFinally(
627- withState(block),
628- withState(finalizer),
629- outerSuccessHandler(_)(state)
630- )
631- }
632- }
633-
634- object TryFinally extends LowPriorityTryFinally {
635-
636- implicit def futureTryFinally [BlockValue , OuterValue ](implicit
637- executionContext : ExecutionContext
638- ): TryFinally [BlockValue , Future [OuterValue ], Future [BlockValue ], Future [
639- Unit
640- ]] = {
641- (
642- block : Future [BlockValue ] !! BlockValue ,
643- finalizer : Future [Unit ] !! Unit ,
644- outerSuccessHandler : BlockValue => Future [OuterValue ]
645- ) =>
646- @ inline
647- def injectFinalizer [A ](f : Unit => Future [A ]): Future [A ] = {
648- catchNativeException(finalizer).flatMap(f)
649- }
650-
651- catchNativeException(block)
652- .recoverWith { case e : Throwable =>
653- injectFinalizer { (_ : Unit ) =>
654- Future .failed(e)
655- }
656- }
657- .flatMap { a =>
658- injectFinalizer { (_ : Unit ) =>
659- outerSuccessHandler(a)
660- }
661- }
662- }
663-
664- implicit def throwableContinuationTryFinally [LeftDomain , Value ]: TryFinally [
665- Value ,
666- LeftDomain !! Throwable ,
667- LeftDomain !! Throwable ,
668- LeftDomain !! Throwable
669- ] = { (block, finalizer, outerSuccessHandler) => outerFailureHandler =>
670- @ inline
671- def injectFinalizer (
672- finalizerHandler : Unit => LeftDomain !! Throwable
673- ): LeftDomain = {
674- locally {
675- try {
676- finalizer(finalizerHandler)
677- } catch {
678- case NonFatal (e) =>
679- return outerFailureHandler(e)
680- }
681- }(outerFailureHandler)
682- }
683-
684- @ inline
685- def hookedFailureHandler (e : Throwable ) =
686- injectFinalizer { (_ : Unit ) =>
687- _(e)
688- }
689-
690- def runBlock (): LeftDomain = {
691- (try {
692- block { value => hookedFailureHandler =>
693- injectFinalizer { (_ : Unit ) =>
694- outerSuccessHandler(value)
695- }
696- }
697- } catch {
698- case NonFatal (e) =>
699- return hookedFailureHandler(e)
700- })(hookedFailureHandler)
701- }
702- runBlock()
703- }
704- }
705-
706400 @ FunctionalInterface
707401 trait Lift [From , + To ] extends (From => To )
708402 private [dsl] trait LowPriorityLift0 { this : Lift .type =>
0 commit comments