Skip to content
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

[JS] Add ConditionalWeakTable #3782

Merged
merged 3 commits into from
Mar 20, 2024

Conversation

chkn
Copy link
Contributor

@chkn chkn commented Mar 12, 2024

Adds an implementation of ConditionalWeakTable for JS/TS using WeakMap.

Missing/different behavior to .NET:

  • The GetOrCreateValue method is not implemented. It might be possible to invoke the default constructor of the value type if the type info for the generic argument is saved when the ConditionalWeakTable is constructed, but I did not implement that.
  • It is not iterable. Though WeakMap is not enumerable, it may be possible to support this by keeping an array of WeakRefs in addition to the WeakMap. But this seems like a lot of overhead, especially since the .NET version only implements IEnumerable as an explicit implementation.
  • You cannot use JS primitive types as keys, including strings. The workaround is to manually box them using, e.g. new String(str)

With this PR, any WeakMap obtained from JS should be usable in F# as a ConditionalWeakTable, but the Clear method will only work on ConditionalWeakTables constructed from F#.

@MangelMaxime
Copy link
Member

Hello thanks for the PR,

I am not familiar with ConditionalWeakTable so I not use what they are used for. Do you need ConditionalWeakTable for a specific usage?

It seems like the user needs to be really careful when using it the implementation from this PR, and have a high change of shooting himself in the foot for this reason if we are to accept this PR, it would be nice to also have a new paragraph on Fable documentation here.

Could you please write a documentation for it, with examples close to a real usage? So people can refers to it if needed?

@chkn
Copy link
Contributor Author

chkn commented Mar 19, 2024

I am not familiar with ConditionalWeakTable so I not use what they are used for. Do you need ConditionalWeakTable for a specific usage?

It is analogous to WeakMap in JS. Basically, a map data structure where keys are always compared by reference, and being present in the map doesn't keep key objects alive. If the key is garbage collected, the key-value pair is removed from the map. It's handy for adding "expando" properties to objects without leaking them.

It seems like the user needs to be really careful when using it the implementation from this PR, and have a high change of shooting himself in the foot for this reason if we are to accept this PR, it would be nice to also have a new paragraph on Fable documentation here.

Could you please write a documentation for it, with examples close to a real usage? So people can refers to it if needed?

Sure, how about this? fable-compiler/fable-compiler.github.io#182

@MangelMaxime MangelMaxime merged commit 31406dc into fable-compiler:main Mar 20, 2024
19 checks passed
@MangelMaxime
Copy link
Member

Thank for the explanation and PR.

It has been released as part of Fable 4.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants