diff --git a/src/FSharpPlus/Control/Monad.fs b/src/FSharpPlus/Control/Monad.fs index 6ba6a8f4c..7c18ec954 100644 --- a/src/FSharpPlus/Control/Monad.fs +++ b/src/FSharpPlus/Control/Monad.fs @@ -206,7 +206,7 @@ type TryWith = static member inline TryWith (_: unit -> ^t when ^t: null and ^t: struct, _ : exn -> 't , _: Default1, _) = () static member TryWith (computation: unit -> seq<_> , catchHandler: exn -> seq<_> , _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) - static member TryWith (computation: unit -> FSharpPlus.Data.NonEmptySeq<_>, catchHandler: exn -> FSharpPlus.Data.NonEmptySeq<_>, _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> FSharpPlus.Data.NonEmptySeq.unsafeOfSeq + static member TryWith (computation: unit -> NonEmptySeq<_>, catchHandler: exn -> NonEmptySeq<_>, _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> NonEmptySeq.unsafeOfSeq static member TryWith (computation: unit -> 'R -> _ , catchHandler: exn -> 'R -> _ , _: Default2, _) = (fun s -> try (computation ()) s with e -> catchHandler e s) : 'R ->_ static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWith , _) = async.TryWith ((computation ()), catchHandler) #if !FABLE_COMPILER @@ -218,6 +218,10 @@ type TryWith = let inline call (mthd: 'M, input: unit -> 'I, _output: 'R, h: exn -> 'I) = ((^M or ^I) : (static member TryWith : _*_*_*_ -> _) input, h, mthd, False) call (Unchecked.defaultof, (fun () -> source), Unchecked.defaultof<'``Monad<'T>``>, f) + static member inline InvokeFromOtherMonad (source: unit ->'``Monad<'T>``) (f: exn -> '``Monad<'T>``) : '``Monad<'T>`` = + let inline call (mthd: 'M, input: unit -> 'I, _output: 'R, h: exn -> 'I) = ((^M or ^I) : (static member TryWith : _*_*_*_ -> _) input, h, mthd, True) + call (Unchecked.defaultof, source, Unchecked.defaultof<'``Monad<'T>``>, f) + static member inline InvokeForWhile (source: '``Monad<'T>``) (f: exn -> '``Monad<'T>``) : '``Monad<'T>`` = let inline call (mthd: 'M, input: unit -> 'I, _output: 'R, h: exn -> 'I) = ((^M or ^I) : (static member TryWith : _*_*_*_ -> _) input, h, mthd, While) call (Unchecked.defaultof, (fun () -> source), Unchecked.defaultof<'``Monad<'T>``>, f) @@ -237,7 +241,7 @@ type TryWithS = static member inline TryWith (_: unit -> ^t when ^t: null and ^t: struct, _ : exn -> 't , _: Default1, _) = () static member TryWith (computation: unit -> seq<_> , catchHandler: exn -> seq<_> , _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) - static member TryWith (computation: unit -> FSharpPlus.Data.NonEmptySeq<_>, catchHandler: exn -> FSharpPlus.Data.NonEmptySeq<_>, _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> FSharpPlus.Data.NonEmptySeq.unsafeOfSeq + static member TryWith (computation: unit -> NonEmptySeq<_>, catchHandler: exn -> NonEmptySeq<_>, _: Default2, _) = seq (try (Seq.toArray (computation ())) with e -> Seq.toArray (catchHandler e)) |> NonEmptySeq.unsafeOfSeq static member TryWith (computation: unit -> 'R -> _ , catchHandler: exn -> 'R -> _ , _: Default2, _) = (fun s -> try (computation ()) s with e -> catchHandler e s) : 'R ->_ static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWithS, _) = async.TryWith ((computation ()), catchHandler) #if !FABLE_COMPILER diff --git a/src/FSharpPlus/Data/Reader.fs b/src/FSharpPlus/Data/Reader.fs index 5225ad717..b11d16cd4 100644 --- a/src/FSharpPlus/Data/Reader.fs +++ b/src/FSharpPlus/Data/Reader.fs @@ -220,7 +220,7 @@ type ReaderT<'r, 'monad, 't> with static member inline Zip (x: ReaderT<'S, 'Monad, 'T>, y: ReaderT<'S, 'Monad, 'U>) = ReaderT.zip x y static member inline TryWith (source: ReaderT<'R, 'Monad, 'T>, f: exn -> ReaderT<'R, 'Monad, 'T>) = - ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryWithS.Invoke (fun () -> (ReaderT.run source s : '``Monad<'T>``)) (fun x -> ReaderT.run (f x) s)) + ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryWith.InvokeFromOtherMonad (fun () -> (ReaderT.run source s : '``Monad<'T>``)) (fun x -> ReaderT.run (f x) s)) static member inline TryFinally (computation: ReaderT<'R, 'Monad, 'T>, f) = ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryFinallyS.Invoke (fun () -> ReaderT.run computation s) f) diff --git a/src/FSharpPlus/Data/State.fs b/src/FSharpPlus/Data/State.fs index 27c2654ff..c7af6f0e9 100644 --- a/src/FSharpPlus/Data/State.fs +++ b/src/FSharpPlus/Data/State.fs @@ -234,7 +234,7 @@ type StateT<'s, 'monad, 't> with static member inline Zip (x: StateT<'S, 'Monad, 'T>, y: StateT<'S, 'Monad, 'U>) = StateT.zip x y static member inline TryWith (source: StateT<'S, 'Monad, 'T>, f: exn -> StateT<'S, 'Monad, 'T>) = - StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryWithS.Invoke (fun () -> (StateT.run source s : '``Monad<'T * 'S>`` )) (fun x -> StateT.run (f x) s)) + StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryWith.InvokeFromOtherMonad (fun () -> (StateT.run source s : '``Monad<'T * 'S>`` )) (fun x -> StateT.run (f x) s)) static member inline TryFinally (computation: StateT<'S,'Monad,'T>, f) = StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryFinallyS.Invoke (fun () -> StateT.run computation s) f)