Skip to content

Commit 9108197

Browse files
committed
Merge remote-tracking branch 'manofstick/manofstick-seq-composer' into manofstick
# Conflicts: # src/fsharp/FSharp.Core/seq.fs
2 parents 384579c + 42c570c commit 9108197

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/fsharp/FSharp.Core/seq.fs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,10 @@ namespace Microsoft.FSharp.Collections
827827

828828
let inline ComposeFilter f g x = f x && g x
829829

830+
let inline UpcastEnumerable (t:#IEnumerable<'T>) : IEnumerable<'T> = (# "" t : IEnumerable<'T> #)
831+
let inline UpcastEnumerator (t:#IEnumerator<'T>) : IEnumerator<'T> = (# "" t : IEnumerator<'T> #)
832+
let inline UpcastEnumeratorNonGeneric (t:#IEnumerator) : IEnumerator = (# "" t : IEnumerator #)
833+
830834
type [<AbstractClass>] SeqComponent<'T,'U> () =
831835
abstract ProcessNext : input:'T * halt:byref<bool> * output:byref<'U> -> bool
832836
abstract OnComplete : unit -> unit
@@ -1093,7 +1097,7 @@ namespace Microsoft.FSharp.Collections
10931097
| _ -> source.Dispose (); source <- Unchecked.defaultof<_>
10941098

10951099
interface IEnumerator with
1096-
member this.Current : obj = box (this:>IEnumerator<'U>).Current
1100+
member this.Current : obj = box (Helpers.UpcastEnumerator this).Current
10971101
member __.MoveNext () =
10981102
state <- SeqProcessNextStates.InProcess
10991103
moveNext ()
@@ -1129,7 +1133,7 @@ namespace Microsoft.FSharp.Collections
11291133
member __.Dispose() : unit = ()
11301134

11311135
interface IEnumerator with
1132-
member this.Current : obj = box (this:>IEnumerator<'U>).Current
1136+
member this.Current : obj = box ((Helpers.UpcastEnumerator this)).Current
11331137
member __.MoveNext () =
11341138
state <- SeqProcessNextStates.InProcess
11351139
moveNext ()
@@ -1186,16 +1190,16 @@ namespace Microsoft.FSharp.Collections
11861190
inherit ComposableEnumerable<'U>()
11871191

11881192
let getEnumerator () : IEnumerator<'U> =
1189-
upcast (new SeqComposedEnumerator<'T,'U>(enumerable.GetEnumerator(), current ()))
1193+
Helpers.UpcastEnumerator (new SeqComposedEnumerator<'T,'U>(enumerable.GetEnumerator(), current ()))
11901194

11911195
interface IEnumerable with
1192-
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
1196+
member this.GetEnumerator () : IEnumerator = Helpers.UpcastEnumeratorNonGeneric (getEnumerator ())
11931197

11941198
interface IEnumerable<'U> with
11951199
member this.GetEnumerator () : IEnumerator<'U> = getEnumerator ()
11961200

11971201
override __.Compose (next:unit->SeqComponent<'U,'V>) : IEnumerable<'V> =
1198-
upcast new SeqEnumerable<'T,'V>(enumerable, fun () -> (current ()).Composer (next ()))
1202+
Helpers.UpcastEnumerable (new SeqEnumerable<'T,'V>(enumerable, fun () -> (current ()).Composer (next ())))
11991203

12001204
// interface ISeqEnumerable<'U> with
12011205
// member this.Fold<'State> (folder:'State->'U->'State) (initialState:'State) : 'State =
@@ -1217,16 +1221,16 @@ namespace Microsoft.FSharp.Collections
12171221
inherit ComposableEnumerable<'U>()
12181222

12191223
let getEnumerator () : IEnumerator<'U> =
1220-
upcast (new ArrayComposedEnumerator<'T,'U>(array, current ()))
1224+
Helpers.UpcastEnumerator (new ArrayComposedEnumerator<'T,'U>(array, current ()))
12211225

12221226
interface IEnumerable with
1223-
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
1227+
member this.GetEnumerator () : IEnumerator = Helpers.UpcastEnumeratorNonGeneric (getEnumerator ())
12241228

12251229
interface IEnumerable<'U> with
12261230
member this.GetEnumerator () : IEnumerator<'U> = getEnumerator ()
12271231

12281232
override __.Compose (next:unit->SeqComponent<'U,'V>) : IEnumerable<'V> =
1229-
upcast new SeqArrayEnumerable<'T,'V>(array, fun () -> (current ()).Composer (next ()))
1233+
Helpers.UpcastEnumerable (new SeqArrayEnumerable<'T,'V>(array, fun () -> (current ()).Composer (next ())))
12301234

12311235
// interface ISeqEnumerable<'U> with
12321236
// member this.Fold<'State> (folder:'State->'U->'State) (initialState:'State) : 'State =
@@ -1249,16 +1253,16 @@ namespace Microsoft.FSharp.Collections
12491253
inherit ComposableEnumerable<'U>()
12501254

12511255
let getEnumerator () : IEnumerator<'U> =
1252-
upcast (new ListComposedEnumerator<'T,'U>(alist, current ()))
1256+
Helpers.UpcastEnumerator (new ListComposedEnumerator<'T,'U>(alist, current ()))
12531257

12541258
interface IEnumerable with
1255-
member this.GetEnumerator () : IEnumerator = upcast (getEnumerator ())
1259+
member this.GetEnumerator () : IEnumerator = Helpers.UpcastEnumeratorNonGeneric (getEnumerator ())
12561260

12571261
interface IEnumerable<'U> with
12581262
member this.GetEnumerator () : IEnumerator<'U> = getEnumerator ()
12591263

12601264
override __.Compose (next:unit->SeqComponent<'U,'V>) : IEnumerable<'V> =
1261-
upcast new SeqListEnumerable<'T,'V>(alist, fun () -> (current ()).Composer (next ()))
1265+
Helpers.UpcastEnumerable (new SeqListEnumerable<'T,'V>(alist, fun () -> (current ()).Composer (next ())))
12621266

12631267

12641268
#if FX_NO_ICLONEABLE
@@ -1384,9 +1388,9 @@ namespace Microsoft.FSharp.Collections
13841388
checkNonNull "source" source
13851389
match source with
13861390
| :? SeqComposer.ComposableEnumerable<'T> as s -> s.Compose createSeqComponent
1387-
| :? array<'T> as a -> upcast (new SeqComposer.SeqArrayEnumerable<_,_>(a, createSeqComponent))
1388-
| :? list<'T> as a -> upcast (new SeqComposer.SeqListEnumerable<_,_>(a, createSeqComponent))
1389-
| _ -> upcast (new SeqComposer.SeqEnumerable<_,_>(source, createSeqComponent))
1391+
| :? array<'T> as a -> SeqComposer.Helpers.UpcastEnumerable (new SeqComposer.SeqArrayEnumerable<_,_>(a, createSeqComponent))
1392+
| :? list<'T> as a -> SeqComposer.Helpers.UpcastEnumerable (new SeqComposer.SeqListEnumerable<_,_>(a, createSeqComponent))
1393+
| _ -> SeqComposer.Helpers.UpcastEnumerable (new SeqComposer.SeqEnumerable<_,_>(source, createSeqComponent))
13901394

13911395
let private seqFactoryForImmutable seqComponent (source:seq<'T>) =
13921396
source |> seqFactory (fun () -> seqComponent)

0 commit comments

Comments
 (0)