Skip to content

Commit

Permalink
Split TryFinally
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Jul 8, 2022
1 parent 29396ff commit 407322f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/FSharpPlus/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module GenericBuilders =
member __.Delay expr = expr : unit -> '``Monad<'T>``
member __.Run f = f () : '``monad<'t>``
member inline __.TryWith (expr, handler) = TryWith.InvokeForStrict expr handler : '``Monad<'T>``
member inline __.TryFinally (expr, compensation) = TryFinally.InvokeForStrict expr compensation : '``Monad<'T>``
member inline __.TryFinally (expr, compensation) = TryFinallyS.Invoke expr compensation : '``Monad<'T>``

member inline __.Using (disposable: #IDisposable, body) = Using.Invoke disposable body

Expand Down
39 changes: 28 additions & 11 deletions src/FSharpPlus/Control/Monad.fs
Original file line number Diff line number Diff line change
Expand Up @@ -239,32 +239,49 @@ type TryFinally =

static member TryFinally ((computation: unit -> Id<_> , compensation: unit -> unit), _: TryFinally, _, _) = try computation () finally compensation ()
static member TryFinally ((computation: unit -> Async<_>, compensation: unit -> unit), _: TryFinally, _, _) = async.TryFinally (computation (), compensation) : Async<_>
#if !FABLE_COMPILER
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally computation compensation : Task<_>
#endif
static member TryFinally ((computation: unit -> Lazy<_> , compensation: unit -> unit), _: TryFinally, _, _) = lazy (try (computation ()).Force () finally compensation ()) : Lazy<_>

static member inline Invoke (source: '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
let inline call (mthd: 'M, input: unit ->'I, _output: 'I, h: unit -> unit) = ((^M or ^I) : (static member TryFinally : (_*_)*_*_*_ -> _) (input, h), mthd, Unchecked.defaultof<TryFinally>, False)
call (Unchecked.defaultof<TryFinally>, (fun () -> source), Unchecked.defaultof<'``Monad<'T>``>, f)

static member inline InvokeForStrict (source: unit ->'``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
let inline call (mthd: 'M, input: unit ->'I, _output: 'I, h: unit -> unit) = ((^M or ^I) : (static member TryFinally : (_*_)*_*_*_ -> _) (input, h), mthd, Unchecked.defaultof<TryFinally>, True)
call (Unchecked.defaultof<TryFinally>, source, Unchecked.defaultof<'``Monad<'T>``>, f)

static member inline InvokeOnInstance (source: '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` = (^``Monad<'T>`` : (static member TryFinally : _*_->_) source, f) : '``Monad<'T>``
static member inline InvokeOnInstance (source: '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` = printfn "Try Finally default 8 for %A" typeof< ^``Monad<'T>``>; (^``Monad<'T>`` : (static member TryFinally : _*_->_) source, f) : '``Monad<'T>``

type TryFinally with
static member inline TryFinally ((computation: unit -> '``Monad<'T>`` , compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: False) = TryFinally.InvokeOnInstance (computation ()) compensation: '``Monad<'T>``
static member inline TryFinally (( _ : unit -> ^t when ^t:null and ^t:struct , _ : unit -> unit), _: Default1, _: TryFinally , _) = ()

type TryFinallyS =
inherit Default1

[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
static member TryFinally ((_: unit -> 'R -> _ , _: unit -> unit), _: Default2 , _, _defaults: False) = raise Internals.Errors.exnUnreachable

static member TryFinally ((computation: unit -> Id<_> , compensation: unit -> unit), _: TryFinallyS, _, _) = try computation () finally compensation ()
#if !FABLE_COMPILER
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinallyS, _, True) = Task.tryFinally computation compensation : Task<_>
#endif

static member inline Invoke (source: unit ->'``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
let inline call (mthd: 'M, input: unit ->'I, _output: 'I, h: unit -> unit) = ((^M or ^I) : (static member TryFinally : (_*_)*_*_*_ -> _) (input, h), mthd, Unchecked.defaultof<TryFinallyS>, True)
call (Unchecked.defaultof<TryFinallyS>, source, Unchecked.defaultof<'``Monad<'T>``>, f)

static member inline InvokeOnInstance (source: unit -> '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` = (^``Monad<'T>`` : (static member TryFinally : _*_->_) source, f) : '``Monad<'T>``

type TryFinallyS with

[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
static member TryFinally ((_: unit -> '``Monad<'T>`` when '``Monad<'T>`` : struct, _: unit -> unit), _: Default3, _: Default2, _defaults: False) = raise Internals.Errors.exnUnreachable

[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
static member TryFinally ((_: unit -> '``Monad<'T>`` when '``Monad<'T>`` : not struct, _: unit -> unit), _: Default3, _: Default1, _defaults: False) = raise Internals.Errors.exnUnreachable

static member TryFinally ((computation: unit -> '``Monad<'T>``, compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: True ) = try computation () finally compensation ()
static member inline TryFinally ((computation: unit -> '``Monad<'T>``, compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: False) = TryFinally.InvokeOnInstance (computation ()) compensation: '``Monad<'T>``
static member inline TryFinally (( _ : unit -> ^t when ^t: null and ^t: struct, _: unit -> unit), _: Default1, _ , _ ) = ()
static member TryFinally ((computation: unit -> '``Monad<'T>`` when '``Monad<'T>`` : struct, compensation: unit -> unit), _: Default3, _: Default2, _defaults: True) = try computation () finally compensation ()
static member TryFinally ((computation: unit -> '``Monad<'T>`` when '``Monad<'T>`` : not struct, compensation: unit -> unit), _: Default3, _: Default1, _defaults: True) = try computation () finally compensation ()

static member inline TryFinally ((computation: unit -> '``Monad<'T>`` , compensation: unit -> unit), _: Default1, _: TryFinallyS, _defaults: _) = TryFinallyS.InvokeOnInstance computation compensation: '``Monad<'T>``
static member inline TryFinally (( _: unit -> ^t when ^t : null and ^t : struct , _ : unit -> unit), _: Default1, _ , _ ) = ()


type Using =
inherit Default1
Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Data/Reader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ type ReaderT<'r, 'monad, 't> with
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryWith.InvokeForStrict (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 -> TryFinally.InvokeForStrict (fun () -> ReaderT.run computation s) f)
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryFinallyS.Invoke (fun () -> ReaderT.run computation s) f)

static member inline Using (resource, f: _ -> ReaderT<'R, 'Monad, 'T>) =
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> Using.Invoke resource (fun x -> ReaderT.run (f x) s))
Expand Down
2 changes: 1 addition & 1 deletion src/FSharpPlus/Data/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ type StateT<'s, 'monad, 't> with
StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryWith.InvokeForStrict (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 -> TryFinally.InvokeForStrict (fun () -> StateT.run computation s) f)
StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryFinallyS.Invoke (fun () -> StateT.run computation s) f)

static member inline Using (resource: 'S, f: _ -> StateT<'S,'Monad,'T>) =
StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> Using.Invoke resource (fun x -> StateT.run (f x) s))
Expand Down

0 comments on commit 407322f

Please sign in to comment.