Skip to content

Commit 3d54f0f

Browse files
authored
Fix seq's TryFinally (#544)
1 parent 5f241f9 commit 3d54f0f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/FSharpPlus/Control/Monad.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ type TryWith =
263263
type TryFinally =
264264
inherit Default1
265265

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

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

308-
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>
309-
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>
308+
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>
309+
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>
310310
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
311311
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Async<'U>, _: Using ) = async.Using (resource, body)
312312
#if !FABLE_COMPILER

tests/FSharpPlus.Tests/ComputationExpressions.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ module ComputationExpressions =
161161

162162
// Check the result
163163
areEquivalent [42] seqValue
164+
165+
// Test proper lazyness in for loops
166+
let source = seq {
167+
yield 1
168+
yield 2
169+
yield (failwith "error !!!"; 2)
170+
yield 3
171+
}
172+
let x:seq<int> = monad.plus { for x in source do yield x }
173+
x.GetEnumerator ()
174+
()
164175

165176
open FsCheck
166177

0 commit comments

Comments
 (0)