Skip to content

Commit

Permalink
Assert more default overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Dec 18, 2023
1 parent 7cbae04 commit 484cff5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
35 changes: 30 additions & 5 deletions src/FSharpPlus/Control/Collection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ open FSharpPlus.Internals
type OfSeq =
inherit Default1

static member inline OfSeq ((x: seq<'t> , _: 'R ), _: Default5) = (^R : (new : seq<'t> -> ^R) x) : 'R
static member inline OfSeq ((x: seq<KeyValuePair<'k,'v>>, _: 'R ), _: Default5) = (^R : (new : seq<'k*'v> -> ^R) (Seq.map (|KeyValue|) x)) : 'R
static member inline OfSeq ((x: seq<'t>, _: 'R), _: Default5) : 'R =
#if TEST_TRACE
Traces.add "OfSeq, Default5-seq<'t>"
#endif
(^R : (new : seq<'t> -> ^R) x)

static member inline OfSeq ((x: seq<KeyValuePair<'k,'v>>, _: 'R), _: Default5) : 'R =
#if TEST_TRACE
Traces.add "OfSeq, Default5-seq<KeyValuePair<'k,'v>>"
#endif
(^R : (new : seq<'k*'v> -> ^R) (Seq.map (|KeyValue|) x))

static member inline OfSeq ((x: seq<'t> , _: '``Foldable'<T>`` ), _: Default4) = x |> Seq.map Return.Invoke |> Sum.Invoke : '``Foldable'<T>``
static member inline OfSeq ((x: seq<'t>, _: '``Foldable'<T>``), _: Default4) : '``Foldable'<T>`` =
#if TEST_TRACE
Traces.add "OfSeq, Default4-seq<'t>"
#endif
x |> Seq.map Return.Invoke |> Sum.Invoke

static member OfSeq ((x: seq<'t> , _: seq<'t> ), _: Default3) = x
static member OfSeq ((x: seq<'t> , _: ICollection<'t> ), _: Default3) = let d = ResizeArray () in Seq.iter d.Add x; d :> ICollection<'t>
Expand All @@ -34,7 +47,13 @@ type OfSeq =
static member OfSeq ((x: seq<'t> , _: IReadOnlyCollection<'t> ), _: Default3) = IReadOnlyCollection.ofSeq x
#endif

static member inline OfSeq ((x: seq<'t> , _: 'F ), _: Default2) = let c = new 'F () in (Seq.iter (fun t -> ( ^F : (member Add : 't -> ^R) c, t) |> ignore) x); c
static member inline OfSeq ((x: seq<'t>, _: 'F), _: Default2) =
#if TEST_TRACE
Traces.add "OfSeq, Default2-#Add"
#endif
let coll = new 'F ()
(Seq.iter (fun t -> (^F : (member Add : 't -> ^R) coll, t) |> ignore) x)
coll

#if !FABLE_COMPILER
static member OfSeq ((x: seq<'t> , _: 'T when 'T :> ICollection<'t> ), _: Default1) = let d = new 'T () in x |> Seq.iter d.Add; d
Expand Down Expand Up @@ -83,7 +102,13 @@ type OfList =
#endif
static member OfList ((x: list<'t> , _: IReadOnlyCollection<'t> ), _: Default4) = IReadOnlyCollection.ofSeq x

static member inline OfList ((x: list<'t> , _: 'F ), _: Default2) = let c = new 'F () in (List.iter (fun t -> ( ^F : (member Add : 't -> ^R) c, t) |> ignore) x); c
static member inline OfList ((x: list<'t>, _: 'F), _: Default2) =
#if TEST_TRACE
Traces.add "OfList, Default2-#Add"
#endif
let coll = new 'F ()
List.iter (fun t -> (^F : (member Add : 't -> ^R) coll, t) |> ignore) x
coll

#if !FABLE_COMPILER
static member OfList ((x: list<'t> , _: 'T when 'T :> ICollection<'t> ), _: Default1) = let d = new 'T () in x |> List.iter d.Add; d
Expand Down
58 changes: 53 additions & 5 deletions tests/FSharpPlus.Tests/Collections.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Collections =

#if TEST_TRACE
CollectionAssert.AreEqual (["ChunkBy, list<'T>"], Traces.get())
#endif
#endif


let testCollections =
Expand All @@ -52,6 +52,11 @@ module Collections =


let testSeqConversions =

#if TEST_TRACE
Traces.reset()
#endif

let sk: Generic.Stack<_> = ofSeq { 1 .. 3 }
let sg: string = ofSeq {'1'..'3'} // but it will come back as seq<char>
let sb: Text.StringBuilder = ofSeq {'1'..'3'} // but it will come back as seq<char>
Expand All @@ -76,12 +81,29 @@ module Collections =
let _r: IReadOnlyDictionary<_,_> = ofSeq (seq [KeyValuePair(1, "One"); KeyValuePair(2, "Two")])
let rc: IReadOnlyCollection<_> = ofSeq (seq [2..7])
let ut: Hashtable = ofSeq (seq [1,'1';2, '2';3,'3']) // but it will come back as seq<obj>

#if TEST_TRACE
CollectionAssert.AreEqual ([], Traces.get())
#endif

let al: ArrayList = ofSeq (seq ["1";"2";"3"]) // but it will come back as seq<obj>
let us: SortedList = ofSeq (seq [4,'2';3,'4']) // but it will come back as seq<obj>
#if TEST_TRACE
CollectionAssert.AreEqual (["OfSeq, Default2-#Add"], Traces.get())
#endif

let cc: BlockingCollection<_> = ofSeq {'1'..'3'} // but it will come back as seq<obj>
#if TEST_TRACE
CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get())
#endif

let cb: ConcurrentBag<_> = ofSeq {'1'..'3'}
#if TEST_TRACE
CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get())
#endif

let us: SortedList = ofSeq (seq [4,'2';3,'4']) // but it will come back as seq<obj>
let cd: ConcurrentDictionary<_,_> = ofSeq (seq [(1, "One"); (2, "Two")]) // but it will come back as ...
let _cd:ConcurrentDictionary<_,_> = ofSeq (seq [KeyValuePair(1, "One"); KeyValuePair(2, "Two")])
let cb: ConcurrentBag<_> = ofSeq {'1'..'3'}

// now go back
let _sk' = toSeq sk
Expand Down Expand Up @@ -119,7 +141,15 @@ module Collections =
let _cols = columns |> toList |> map (fun x -> x.ColumnName)

// Defaults

#if TEST_TRACE
CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get())
#endif

let _12: WrappedListI<_> = seq [1;2] |> ofSeq
#if TEST_TRACE
CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default4-seq<'t>"], Traces.get())
#endif

()

Expand Down Expand Up @@ -150,12 +180,30 @@ module Collections =
let _r: IReadOnlyDictionary<_,_> = ofList ([KeyValuePair(1, "One"); KeyValuePair(2, "Two")])
let rc: IReadOnlyCollection<_> = ofList ([2..5])
let ut: Hashtable = ofList ([1,'1';2, '2';3,'3']) // but it will come back as seq<obj>

#if TEST_TRACE
CollectionAssert.AreEqual ([], Traces.get())
#endif

let al: ArrayList = ofList (["1";"2";"3"]) // but it will come back as seq<obj>
#if TEST_TRACE
CollectionAssert.AreEqual (["OfList, Default2-#Add"], Traces.get())
#endif

let cc: BlockingCollection<_> = ofList ['1'..'3'] // but it will come back as seq<obj>
#if TEST_TRACE
CollectionAssert.AreEqual (["OfList, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get())
#endif

let cb: ConcurrentBag<_> = ofList ['1'..'3']
#if TEST_TRACE
CollectionAssert.AreEqual (["OfList, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get())
#endif


let us: SortedList = ofList ([4,'2';3,'4']) // but it will come back as seq<obj>
let cc: BlockingCollection<_> = ofList ['1'..'3'] // but it will come back as seq<obj>
let cd: ConcurrentDictionary<_,_> = ofList ([(1, "One"); (2, "Two")]) // but it will come back as ...
let _cd:ConcurrentDictionary<_,_> = ofList ([KeyValuePair(1, "One"); KeyValuePair(2, "Two")])
let cb: ConcurrentBag<_> = ofList ['1'..'3']

// now go back
let _sk' = toList sk
Expand Down

0 comments on commit 484cff5

Please sign in to comment.