You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When comparing two references to a nullary union case for physical equality, they are considered identical in fsc, but distinct in fable. Checking the generated code, this seems to be due to the fact that they are represented as classes, and each reference generates a distinct call to the constructor, instead of reusing a single instance.
typeX= A | B | C
// true for fsc, false for fable
LanguagePrimitives.PhysicalEquality X.A X.A
Attempting to work around this by using either Erase or StringEnum introduces issues with reflection, since the type is erased to String, making reflection techniques keyed on type conflate these types with plain Strings, as discussed in this Thoth.Json issue thoth-org/Thoth.Json#203. Using TypeScriptTaggedUnion has the same issue of generating distinct objects per call site
{type: "a",}==={type: "a",};
One workaround that does work is to write a let for each case, effectively instantiating a singleton for each instance of the DU:
typeX= A | B | C
moduleX =letA= A
letB= B
letC= C
But you need to make sure everything that could work with the constructors such as Thoth decoders references these singletons:
letdecoder:Thoth.Json.Decoder<State>=letdecoder= Decode.Auto.generateDecoder<State>()
decoder
|> Decode.map (function| On -> On
| Off -> Off)
Could the compiler generate these singleton values and reference them instead?
Description
When comparing two references to a nullary union case for physical equality, they are considered identical in fsc, but distinct in fable. Checking the generated code, this seems to be due to the fact that they are represented as classes, and each reference generates a distinct call to the constructor, instead of reusing a single instance.
Attempting to work around this by using either
Erase
orStringEnum
introduces issues with reflection, since the type is erased to String, making reflection techniques keyed on type conflate these types with plain Strings, as discussed in this Thoth.Json issue thoth-org/Thoth.Json#203. UsingTypeScriptTaggedUnion
has the same issue of generating distinct objects per call siteOne workaround that does work is to write a let for each case, effectively instantiating a singleton for each instance of the DU:
But you need to make sure everything that could work with the constructors such as Thoth decoders references these singletons:
Could the compiler generate these singleton values and reference them instead?
Repro code
https://fable.io/repl/#?code=FDAuE8AcFMAIA1YF5YEFYB9YCFOwMIgC2A9gCYCuANnIisLLDaGsmiM7NAI4CMb8AHToUQ1ME48ATGwAyAQwB2Acwrzl0AAoAnAJZFdoXQDdoAZ0GaAFuDO6AxvKoBRbmqqHwCYd-GTuAMxySqrqWnoGRqYW1rYOTq7unt64QtjAkHqKoABmirAARACkAEawpeUlBVx8NTI8AUA&html=Q&css=Q
Expected and actual results
Please provide the expected and actual results.
Related information
4.18.0
The text was updated successfully, but these errors were encountered: