From c955c73f5558b39eb881a454e68c53946fb8f2b2 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 17 Sep 2024 21:10:13 +0200 Subject: [PATCH] Fix issue with iteration of dictionaries --- src/FSharpPlus/Control/Functor.fs | 3 ++- tests/FSharpPlus.Tests/General.fs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/FSharpPlus/Control/Functor.fs b/src/FSharpPlus/Control/Functor.fs index 70609fb6b..bd25869d4 100644 --- a/src/FSharpPlus/Control/Functor.fs +++ b/src/FSharpPlus/Control/Functor.fs @@ -45,7 +45,8 @@ type Iterate = static member Iterate (x: Choice<'T, 'E> , action) = match x with Choice1Of2 x -> action x | _ -> () static member Iterate (KeyValue(_: 'Key, x: 'T), action) = action x : unit static member Iterate (x: Map<'Key,'T> , action) = Map.iter (const' action) x - static member Iterate (x: Dictionary<'Key, 'T> , action) = Dictionary.iterValues action x + static member Iterate (x: IDictionary<'Key, 'T>, action) = Dict.iterValues action x + static member Iterate (x: IReadOnlyDictionary<'Key, 'T>, action) = IReadOnlyDictionary.iterValues action x static member Iterate (x: _ ResizeArray , action) = ResizeArray.iter action x // Restricted diff --git a/tests/FSharpPlus.Tests/General.fs b/tests/FSharpPlus.Tests/General.fs index 6b393fe27..1f330e1f2 100644 --- a/tests/FSharpPlus.Tests/General.fs +++ b/tests/FSharpPlus.Tests/General.fs @@ -487,6 +487,21 @@ module Functor = let nel = zip (NonEmptyList.ofList [1; 2]) (NonEmptyList.ofList ["a"; "b"; "c"]) CollectionAssert.AreEqual (NonEmptyList.ofList [1,"a"; 2,"b"], nel) + [] + let iterTests () = + let di = new Dictionary() + di.Add (1, 2) + di.Add (3, 4) + let id = dict [1, 2; 3, 4] + let ir = readOnlyDict [1, 2; 3, 4] + + let r = ResizeArray [] + + iter (fun x -> r.Add (string x)) di + iter (fun x -> r.Add (string x)) id + iter (fun x -> r.Add (string x)) ir + CollectionAssert.AreEqual (ResizeArray ["2"; "4"; "2"; "4"; "2"; "4"], r) + module Foldable =