-
Notifications
You must be signed in to change notification settings - Fork 847
Implement System.Collections.IDictionary for dict #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dba8190
0fcad7f
e51598a
50a2e40
1ea9629
55aadca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,17 @@ module ExtraTopLevelOperators = | |
| t.[RuntimeHelpers.StructBox(k)] <- v | ||
| let d = (t :> IDictionary<_,_>) | ||
| let c = (t :> ICollection<_>) | ||
|
|
||
| let dictEnumerator (a: System.Collections.Generic.IEnumerator<System.Collections.DictionaryEntry>) = | ||
| { new System.Collections.IDictionaryEnumerator with | ||
| member x.Current = a.Current :> obj | ||
| member x.Entry = a.Current | ||
| member x.Key = x.Entry.Key | ||
| member x.Value = x.Entry.Value | ||
| member x.MoveNext () = a.MoveNext() | ||
| member x.Reset () = a.Reset() | ||
| } | ||
|
|
||
| // Give a read-only view of the dictionary | ||
| { new IDictionary<'Key, 'T> with | ||
| member s.Item | ||
|
|
@@ -70,6 +81,78 @@ module ExtraTopLevelOperators = | |
| let key = RuntimeHelpers.StructBox(k) | ||
| if d.ContainsKey(key) then (r <- d.[key]; true) else false | ||
| member s.Remove(k : 'Key) = (raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))) : bool) | ||
| #if FX_NO_READONLY_COLLECTIONS | ||
| #else | ||
| interface IReadOnlyDictionary<'Key, 'T> with | ||
| member s.Item | ||
| with get x = d.[RuntimeHelpers.StructBox(x)] | ||
| member s.Keys = | ||
| let keys = d.Keys | ||
| { new IEnumerable<'Key> with | ||
| member s.GetEnumerator() = (keys |> Seq.map (fun v -> v.Value)).GetEnumerator() | ||
| interface System.Collections.IEnumerable with | ||
| member s.GetEnumerator() = ((keys |> Seq.map (fun v -> v.Value)) :> System.Collections.IEnumerable).GetEnumerator() } | ||
|
|
||
| member s.Values = | ||
| { new IEnumerable<'T> with | ||
| member s.GetEnumerator() = d.Values.GetEnumerator() | ||
| interface System.Collections.IEnumerable with | ||
| member s.GetEnumerator() = (d.Values :> System.Collections.IEnumerable).GetEnumerator() } | ||
| member s.ContainsKey(k) = d.ContainsKey(RuntimeHelpers.StructBox(k)) | ||
| member s.TryGetValue(k,r) = | ||
| let key = RuntimeHelpers.StructBox(k) | ||
| if d.ContainsKey(key) then (r <- d.[key]; true) else false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type DictWrapper<'k,'v>(d : Dictionary<_,_>) =
member __.TryGetValue(a : 'k, [<System.Runtime.InteropServices.Out>]b : 'v byref) =
d.TryGetValue(a, &b)Might as well fix above, too. |
||
| interface IReadOnlyCollection<KeyValuePair<'Key, 'T>> with | ||
| member s.Count = c.Count | ||
| #endif | ||
| interface System.Collections.IDictionary with | ||
| member s.IsReadOnly = true | ||
| member s.IsFixedSize = true | ||
| member s.IsSynchronized = true | ||
| member s.Item | ||
| with get x = d.[RuntimeHelpers.StructBox(x :?> 'Key)] :> obj | ||
| and set x v = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))) | ||
| member s.Keys = | ||
| let keys = d.Keys | ||
| { new System.Collections.ICollection with | ||
| member s.CopyTo(arr,i) = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementer of Example impl here. |
||
| let mutable n = 0 | ||
| for k in keys do | ||
| arr.SetValue(k.Value,i+n) | ||
| n <- n + 1 | ||
| member s.IsSynchronized = true | ||
| member s.SyncRoot = (t :> System.Collections.ICollection).SyncRoot | ||
| member s.Count = keys.Count | ||
| interface System.Collections.IEnumerable with | ||
| member s.GetEnumerator() = ((keys |> Seq.map (fun v -> v.Value)) :> System.Collections.IEnumerable).GetEnumerator() } | ||
|
|
||
| member s.Values = | ||
| { new System.Collections.ICollection with | ||
| member s.CopyTo(arr,i) = | ||
| let mutable n = 0 | ||
| for k in d.Values do | ||
| arr.SetValue(k,i+n) | ||
| n <- n + 1 | ||
| member s.IsSynchronized = true | ||
| member s.SyncRoot = (t :> System.Collections.ICollection).SyncRoot | ||
| member s.Count = d.Values.Count | ||
| interface System.Collections.IEnumerable with | ||
| member s.GetEnumerator() = (d.Values :> System.Collections.IEnumerable).GetEnumerator() } | ||
| member s.Count = c.Count | ||
| member s.SyncRoot = (t :> System.Collections.ICollection).SyncRoot | ||
| member s.Add(k,v) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))) | ||
| member s.Contains(k) = d.ContainsKey(RuntimeHelpers.StructBox(k :?> 'Key)) | ||
| member s.GetEnumerator() = | ||
| (c |> Seq.map (fun (KeyValue(k,v)) -> System.Collections.DictionaryEntry(k.Value,v))).GetEnumerator() | ||
| |> dictEnumerator | ||
|
|
||
| member s.Remove(k) = (raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))) : unit) | ||
| member s.Clear() = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))); | ||
| member s.CopyTo(arr,i) = | ||
| let mutable n = 0 | ||
| for (KeyValue(k,v)) in c do | ||
| arr.SetValue(System.Collections.DictionaryEntry(k.Value,v),i+n) | ||
| n <- n + 1 | ||
| interface ICollection<KeyValuePair<'Key, 'T>> with | ||
| member s.Add(x) = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))); | ||
| member s.Clear() = raise (NotSupportedException(SR.GetString(SR.thisValueCannotBeMutated))); | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FX_ATLEAST_45 is not enough? IReadOnlyDictionary msdn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to exclude portable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FSHARP_CORE_PORTABLE? because there are really a lot of defines already.
i mean
FX_ATLEAST_45 && (!FSHARP_CORE_PORTABLE)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if FX_ATLEAST_45 && not FSHARP_CORE_PORTABLE
but why not explicitly exlude with ne flag. seems like a common pattern here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are already lot of defines, and is going to be messier with netcore.
i dont think the best long term is one define per class, instead we should simplify the codebase and build permutations
Another factor is: if you undefine FX_NO_READONLY_COLLECTIONS, that's not enogh to compile if the class doesn't exists in the framework. Is not a pure feature flag (like
WSDLdefine )but that's my idea /cc @dsyme
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use feature-based defines (FX_NO_READONLY_COLLECTIONS) rather than platform defines (FX_ATLEAST_45, FSHARP_CORE_PORTABLE). Granularity of "features" is up to you - merge related ones where possible, split where necessary,
thanks
Don
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx for reply @dsyme nice to know 😄