Skip to content

Commit 9fbae65

Browse files
author
Maxime Mangel
authored
Merge pull request #182 from chkn/dev
[JS] Add docs for ConditionalWeakTable
2 parents 73ee20b + f2fbe28 commit 9fbae65

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

docs/docs/javascript/compatibility.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,30 @@ myClass.Square(); // 25
6868
## .NET Base Class Library
6969

7070
The following classes are translated to JS and most of their methods (static and instance) should be available in Fable.
71-
72-
.NET | JavaScript
73-
--------------------------------------|----------------------------
74-
Numeric Types | number
75-
Arrays | Array / Typed Arrays
76-
Events | fable-core/Event
77-
System.Boolean | boolean
78-
System.Char | string
79-
System.String | string
80-
System.Guid | string
81-
System.TimeSpan | number
82-
System.DateTime | Date
83-
System.DateTimeOffset | Date
84-
System.DateOnly | Date
85-
System.TimeOnly | number
86-
System.Timers.Timer | fable-core/Timer
87-
System.Collections.Generic.List | Array
88-
System.Collections.Generic.HashSet | Set
89-
System.Collections.Generic.Dictionary | Map
90-
System.Text.RegularExpressions.Regex | RegExp
91-
System.Lazy | fable-core/Lazy
92-
System.Random | {}
93-
System.Math | (native JS functions)
71+
72+
.NET | JavaScript
73+
-----------------------------------------------------|----------------------------
74+
Numeric Types | number
75+
Arrays | Array / Typed Arrays
76+
Events | fable-core/Event
77+
System.Boolean | boolean
78+
System.Char | string
79+
System.String | string
80+
System.Guid | string
81+
System.TimeSpan | number
82+
System.DateTime | Date
83+
System.DateTimeOffset | Date
84+
System.DateOnly | Date
85+
System.TimeOnly | number
86+
System.Timers.Timer | fable-core/Timer
87+
System.Collections.Generic.List | Array
88+
System.Collections.Generic.HashSet | Set
89+
System.Collections.Generic.Dictionary | Map
90+
System.Text.RegularExpressions.Regex | RegExp
91+
System.Lazy | fable-core/Lazy
92+
System.Random | {}
93+
System.Math | (native JS functions)
94+
System.Runtime.CompilerServices.ConditionalWeakTable | WeakMap
9495

9596
The following static methods are also available:
9697

@@ -114,6 +115,29 @@ See [Name mangling](/docs/javascript/features.html#name-mangling) for more infor
114115
- Numeric arrays are compiled to [Typed Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) when possible.
115116
- No bound checks for numeric types (unless you do explicit conversions like `byte 500`) nor for array indices.
116117
- `Regex` will always behave as if passed `RegexOptions.ECMAScript` flag (e.g., no negative look-behind or named groups).
118+
- `ConditionalWeakTable` has some limitations: the `GetOrCreateValue` method is not supported, nor is casting it to `IEnumerable` and iterating it. If you want to use a string as a key, you must manually box it in a JS `String` object:
119+
120+
```fsharp
121+
open System.Runtime.CompilerServices
122+
123+
// Only needed for strings as keys; other reference types work fine directly
124+
let inline boxString (str: string): obj =
125+
#if FABLE_COMPILER
126+
Fable.Core.JsInterop.emitJsExpr (str) "new String($0)"
127+
#else
128+
box str
129+
#endif
130+
131+
let table = ConditionalWeakTable()
132+
let key = boxString "my-key"
133+
let value = "something"
134+
table.Add(key, value)
135+
136+
// Use the same `key` object later to get the value..
137+
match table.TryGetValue(key) with
138+
| true, value -> // ...
139+
| _ -> // ...
140+
```
117141

118142
## FSharp.Core
119143

0 commit comments

Comments
 (0)