Skip to content

Commit

Permalink
Merge pull request #182 from chkn/dev
Browse files Browse the repository at this point in the history
[JS] Add docs for ConditionalWeakTable
  • Loading branch information
MangelMaxime authored Mar 20, 2024
2 parents 73ee20b + f2fbe28 commit 9fbae65
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions docs/docs/javascript/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,30 @@ myClass.Square(); // 25
## .NET Base Class Library

The following classes are translated to JS and most of their methods (static and instance) should be available in Fable.

.NET | JavaScript
--------------------------------------|----------------------------
Numeric Types | number
Arrays | Array / Typed Arrays
Events | fable-core/Event
System.Boolean | boolean
System.Char | string
System.String | string
System.Guid | string
System.TimeSpan | number
System.DateTime | Date
System.DateTimeOffset | Date
System.DateOnly | Date
System.TimeOnly | number
System.Timers.Timer | fable-core/Timer
System.Collections.Generic.List | Array
System.Collections.Generic.HashSet | Set
System.Collections.Generic.Dictionary | Map
System.Text.RegularExpressions.Regex | RegExp
System.Lazy | fable-core/Lazy
System.Random | {}
System.Math | (native JS functions)

.NET | JavaScript
-----------------------------------------------------|----------------------------
Numeric Types | number
Arrays | Array / Typed Arrays
Events | fable-core/Event
System.Boolean | boolean
System.Char | string
System.String | string
System.Guid | string
System.TimeSpan | number
System.DateTime | Date
System.DateTimeOffset | Date
System.DateOnly | Date
System.TimeOnly | number
System.Timers.Timer | fable-core/Timer
System.Collections.Generic.List | Array
System.Collections.Generic.HashSet | Set
System.Collections.Generic.Dictionary | Map
System.Text.RegularExpressions.Regex | RegExp
System.Lazy | fable-core/Lazy
System.Random | {}
System.Math | (native JS functions)
System.Runtime.CompilerServices.ConditionalWeakTable | WeakMap

The following static methods are also available:

Expand All @@ -114,6 +115,29 @@ See [Name mangling](/docs/javascript/features.html#name-mangling) for more infor
- Numeric arrays are compiled to [Typed Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) when possible.
- No bound checks for numeric types (unless you do explicit conversions like `byte 500`) nor for array indices.
- `Regex` will always behave as if passed `RegexOptions.ECMAScript` flag (e.g., no negative look-behind or named groups).
- `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:

```fsharp
open System.Runtime.CompilerServices
// Only needed for strings as keys; other reference types work fine directly
let inline boxString (str: string): obj =
#if FABLE_COMPILER
Fable.Core.JsInterop.emitJsExpr (str) "new String($0)"
#else
box str
#endif
let table = ConditionalWeakTable()
let key = boxString "my-key"
let value = "something"
table.Add(key, value)
// Use the same `key` object later to get the value..
match table.TryGetValue(key) with
| true, value -> // ...
| _ -> // ...
```

## FSharp.Core

Expand Down

0 comments on commit 9fbae65

Please sign in to comment.