Skip to content

Commit c3fb4fc

Browse files
committed
Split TryFinally
1 parent b1d191c commit c3fb4fc

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/FSharpPlus/Builders.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module GenericBuilders =
7070
member __.Delay expr = expr : unit -> '``Monad<'T>``
7171
member __.Run f = f () : '``monad<'t>``
7272
member inline __.TryWith (expr, handler) = TryWith.InvokeForStrict expr handler : '``Monad<'T>``
73-
member inline __.TryFinally (expr, compensation) = TryFinally.InvokeForStrict expr compensation : '``Monad<'T>``
73+
member inline __.TryFinally (expr, compensation) = TryFinallyS.Invoke expr compensation : '``Monad<'T>``
7474

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

src/FSharpPlus/Control/Monad.fs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,49 @@ type TryFinally =
239239

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

247244
static member inline Invoke (source: '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
248245
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)
249246
call (Unchecked.defaultof<TryFinally>, (fun () -> source), Unchecked.defaultof<'``Monad<'T>``>, f)
250247

251-
static member inline InvokeForStrict (source: unit ->'``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
252-
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)
253-
call (Unchecked.defaultof<TryFinally>, source, Unchecked.defaultof<'``Monad<'T>``>, f)
254-
255-
static member inline InvokeOnInstance (source: '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` = (^``Monad<'T>`` : (static member TryFinally : _*_->_) source, f) : '``Monad<'T>``
248+
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>``
256249

257250
type TryFinally with
251+
static member inline TryFinally ((computation: unit -> '``Monad<'T>`` , compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: False) = TryFinally.InvokeOnInstance (computation ()) compensation: '``Monad<'T>``
252+
static member inline TryFinally (( _ : unit -> ^t when ^t:null and ^t:struct , _ : unit -> unit), _: Default1, _: TryFinally , _) = ()
253+
254+
type TryFinallyS =
255+
inherit Default1
256+
257+
[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
258+
static member TryFinally ((_: unit -> 'R -> _ , _: unit -> unit), _: Default2 , _, _defaults: False) = raise Internals.Errors.exnUnreachable
259+
260+
static member TryFinally ((computation: unit -> Id<_> , compensation: unit -> unit), _: TryFinallyS, _, _) = try computation () finally compensation ()
261+
#if !FABLE_COMPILER
262+
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinallyS, _, True) = Task.tryFinally computation compensation : Task<_>
263+
#endif
264+
265+
static member inline Invoke (source: unit ->'``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` =
266+
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)
267+
call (Unchecked.defaultof<TryFinallyS>, source, Unchecked.defaultof<'``Monad<'T>``>, f)
268+
269+
static member inline InvokeOnInstance (source: unit -> '``Monad<'T>``) (f: unit -> unit) : '``Monad<'T>`` = (^``Monad<'T>`` : (static member TryFinally : _*_->_) source, f) : '``Monad<'T>``
270+
271+
type TryFinallyS with
258272

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

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

265-
static member TryFinally ((computation: unit -> '``Monad<'T>``, compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: True ) = try computation () finally compensation ()
266-
static member inline TryFinally ((computation: unit -> '``Monad<'T>``, compensation: unit -> unit), _: Default1, _: TryFinally, _defaults: False) = TryFinally.InvokeOnInstance (computation ()) compensation: '``Monad<'T>``
267-
static member inline TryFinally (( _ : unit -> ^t when ^t: null and ^t: struct, _: unit -> unit), _: Default1, _ , _ ) = ()
279+
static member TryFinally ((computation: unit -> '``Monad<'T>`` when '``Monad<'T>`` : struct, compensation: unit -> unit), _: Default3, _: Default2, _defaults: True) = try computation () finally compensation ()
280+
static member TryFinally ((computation: unit -> '``Monad<'T>`` when '``Monad<'T>`` : not struct, compensation: unit -> unit), _: Default3, _: Default1, _defaults: True) = try computation () finally compensation ()
281+
282+
static member inline TryFinally ((computation: unit -> '``Monad<'T>`` , compensation: unit -> unit), _: Default1, _: TryFinallyS, _defaults: _) = TryFinallyS.InvokeOnInstance computation compensation: '``Monad<'T>``
283+
static member inline TryFinally (( _: unit -> ^t when ^t : null and ^t : struct , _ : unit -> unit), _: Default1, _ , _ ) = ()
284+
268285

269286
type Using =
270287
inherit Default1

src/FSharpPlus/Data/Reader.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ type ReaderT<'r, 'monad, 't> with
223223
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))
224224

225225
static member inline TryFinally (computation: ReaderT<'R, 'Monad, 'T>, f) =
226-
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryFinally.InvokeForStrict (fun () -> ReaderT.run computation s) f)
226+
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> TryFinallyS.Invoke (fun () -> ReaderT.run computation s) f)
227227

228228
static member inline Using (resource, f: _ -> ReaderT<'R, 'Monad, 'T>) =
229229
ReaderTOperations.ReaderT<'``Monad<'T>``, 'Monad, 'R, 'T> (fun s -> Using.Invoke resource (fun x -> ReaderT.run (f x) s))

src/FSharpPlus/Data/State.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ type StateT<'s, 'monad, 't> with
237237
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))
238238

239239
static member inline TryFinally (computation: StateT<'S,'Monad,'T>, f) =
240-
StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryFinally.InvokeForStrict (fun () -> StateT.run computation s) f)
240+
StateTOperations.StateT< '``Monad<'T * 'S>``, 'Monad, 'S, 'T> (fun s -> TryFinallyS.Invoke (fun () -> StateT.run computation s) f)
241241

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

0 commit comments

Comments
 (0)