Skip to content

Commit aef7dbf

Browse files
authored
Use a (named) type
1 parent abf8fb3 commit aef7dbf

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,82 @@
11
namespace FSharpPlus
22

3+
[<AutoOpen>]
4+
module Auto =
5+
let icollection value =
6+
{
7+
new ICollection<'t> with
8+
member _.Contains (item: 't) = obj.ReferenceEquals (item, value)
9+
member _.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () :> System.Collections.IEnumerator
10+
member _.GetEnumerator () = (Seq.initInfinite (fun _ -> value)).GetEnumerator () : IEnumerator<'t>
11+
member _.IsReadOnly = true
12+
13+
member _.Add (_item: 't) : unit = raise (System.NotImplementedException())
14+
member _.Clear () : unit = raise (System.NotImplementedException())
15+
member _.CopyTo (_array: 't [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
16+
member _.Count : int = raise (System.NotImplementedException())
17+
member _.Remove (_item: 't): bool = raise (System.NotImplementedException())
18+
}
19+
20+
type DefaultableDict<'TKey, 'TValue>(source: 'TValue) =
21+
interface IDictionary<'TKey, 'TValue> with
22+
member _.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
23+
member _.Count =
24+
if typeof<'TKey>.IsValueType then
25+
if typeof<'TKey> = typeof<bool> then 2
26+
else
27+
let s = sizeof<'TKey>
28+
if s < 4 then pown 2 (sizeof<'TKey> * 8)
29+
else -1
30+
elif typeof<'TKey> = typeof<unit> then 1
31+
else -1
32+
member _.ContainsKey (_key: 'TKey) = true
33+
member _.Contains (item: KeyValuePair<'TKey,'TValue>) = obj.ReferenceEquals (item.Value, source)
34+
member _.GetEnumerator () = invalidOp "Key set is potentially infinite." : System.Collections.IEnumerator
35+
member _.GetEnumerator () = invalidOp "Key set is potentially infinite." : IEnumerator<KeyValuePair<'TKey,'TValue>>
36+
member _.IsReadOnly = true
37+
member _.Values = icollection source
38+
member _.Item
39+
with get (_key: 'TKey) : 'TValue = source
40+
and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
41+
42+
member _.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
43+
member _.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
44+
member _.Clear () : unit = raise (System.NotImplementedException())
45+
member _.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
46+
member _.Keys : ICollection<'TKey> = raise (System.NotImplementedException())
47+
member _.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
48+
member _.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
49+
(*
50+
interface IReadOnlyDictionary<'TKey, 'TValue> with
51+
member _.TryGetValue (_key: 'TKey, value: byref<'TValue>) = value <- source; true
52+
member _.Count =
53+
if typeof<'TKey>.IsValueType then
54+
if typeof<'TKey> = typeof<bool> then 2
55+
else
56+
let s = sizeof<'TKey>
57+
if s < 4 then pown 2 (sizeof<'TKey> * 8)
58+
else -1
59+
elif typeof<'TKey> = typeof<unit> then 1
60+
else -1
61+
member _.ContainsKey (_key: 'TKey) = true
62+
// member _.Contains (item: KeyValuePair<'TKey,'TValue>) = obj.ReferenceEquals (item.Value, source)
63+
member _.GetEnumerator () = invalidOp "Key set is potentially infinite." : System.Collections.IEnumerator
64+
member _.GetEnumerator () = invalidOp "Key set is potentially infinite." : IEnumerator<KeyValuePair<'TKey,'TValue>>
65+
// member _.IsReadOnly = true
66+
member _.Values = icollection source
67+
member _.Item
68+
with get (_key: 'TKey) : 'TValue = source
69+
// and set (_key: 'TKey) (_: 'TValue) : unit = raise (System.NotImplementedException())
70+
71+
// member _.Add (_key: 'TKey, _value: 'TValue) : unit = raise (System.NotImplementedException())
72+
// member _.Add (_item: KeyValuePair<'TKey,'TValue>) : unit = raise (System.NotImplementedException())
73+
// member _.Clear () : unit = raise (System.NotImplementedException())
74+
// member _.CopyTo (_arr: KeyValuePair<'TKey,'TValue> [], _arrayIndex: int) : unit = raise (System.NotImplementedException())
75+
member _.Keys : IEnumerable<'TKey> = raise (System.NotImplementedException())
76+
member _.Remove (_key: 'TKey) : bool = raise (System.NotImplementedException())
77+
member _.Remove (_item: KeyValuePair<'TKey,'TValue>) : bool = raise (System.NotImplementedException())
78+
*)
79+
380
/// Additional operations on IDictionary<'Key, 'Value>
481
[<RequireQualifiedAccess>]
582
module Dict =

0 commit comments

Comments
 (0)