Skip to content

Commit

Permalink
Fix seq's TryFinally (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Apr 3, 2023
1 parent 5f241f9 commit 3d54f0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/FSharpPlus/Control/Monad.fs
Expand Up @@ -263,8 +263,8 @@ type TryWith =
type TryFinally =
inherit Default1

static member TryFinally ((computation: unit -> seq<_> , compensation: unit -> unit), _: Default2, _, _) = seq (try (Seq.toArray (computation ())) finally compensation ())
static member TryFinally ((computation: unit -> NonEmptySeq<_>, compensation: unit -> unit), _: Default2, _, _) = seq (try (Seq.toArray (computation ())) finally compensation ()) |> NonEmptySeq.unsafeOfSeq
static member TryFinally ((computation: unit -> seq<_> , compensation: unit -> unit), _: Default2, _, _) = seq { try for e in computation () do yield e finally compensation () }
static member TryFinally ((computation: unit -> NonEmptySeq<_>, compensation: unit -> unit), _: Default2, _, _) = seq { try for e in computation () do yield e finally compensation () } |> NonEmptySeq.unsafeOfSeq

[<CompilerMessage(MessageTryFinally, CodeTryFinally, IsError = true)>]
static member TryFinally ((_: unit -> 'R -> _ , _: unit -> unit), _: Default2 , _, _defaults: False) = raise Internals.Errors.exnUnreachable
Expand Down Expand Up @@ -305,8 +305,8 @@ type TryFinally with
type Using =
inherit Default1

static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> seq<'U> , _: Using) = seq (try Seq.toArray (body resource) finally if not (isNull (box resource)) then resource.Dispose ()) : seq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> NonEmptySeq<'U>, _: Using) = seq (try Seq.toArray (body resource) finally if not (isNull (box resource)) then resource.Dispose ()) |> NonEmptySeq.unsafeOfSeq : NonEmptySeq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> seq<'U> , _: Using) = seq { try for e in body resource do yield e finally if not (isNull (box resource)) then resource.Dispose () } : seq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> NonEmptySeq<'U>, _: Using) = seq { try for e in body resource do yield e finally if not (isNull (box resource)) then resource.Dispose () } |> NonEmptySeq.unsafeOfSeq : NonEmptySeq<'U>
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> 'R -> 'U , _: Using ) = (fun s -> try body resource s finally if not (isNull (box resource)) then resource.Dispose ()) : 'R->'U
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Async<'U>, _: Using ) = async.Using (resource, body)
#if !FABLE_COMPILER
Expand Down
11 changes: 11 additions & 0 deletions tests/FSharpPlus.Tests/ComputationExpressions.fs
Expand Up @@ -161,6 +161,17 @@ module ComputationExpressions =

// Check the result
areEquivalent [42] seqValue

// Test proper lazyness in for loops
let source = seq {
yield 1
yield 2
yield (failwith "error !!!"; 2)
yield 3
}
let x:seq<int> = monad.plus { for x in source do yield x }
x.GetEnumerator ()
()

open FsCheck

Expand Down

0 comments on commit 3d54f0f

Please sign in to comment.