3 ms·
Looks like it's "just" type-erasure / syntactical sugar. E.g. value types are boxed.
by mwkaufma 6mo ago
Looks like it's "just" type-erasure / syntactical sugar. E.g. value types are boxed.
- AndrewDucker 6mo agoYes, but see the section on custom unions* - you can write non-boxing unions/generators. * https://devblogs.microsoft.com/dotnet/csharp-15-union-types/#custom-unions-for-existing-libraries https://devblogs.microsoft.com/dotnet/csharp-15-union-types/...
- celeries 6mo agoYes, but that's just the default behavior. You can implement your own non-boxing version for performance critical applications.
- algorithmsRcool 6mo agoWhy on earth did they decide boxing by default was a sensible design decision... We have been pushing toward higher performance for years and this is a performance pitfall for unions would are often thought of as being lighter weight than inheritance hierarchies. F# just stores a field-per-case, with the optimization that cases with the same type are unified which is still type safe.
- zigzag312 6mo agoFrom what I've read, this is for the first implementation of unions, to reduce amount of compiler work they need to do. They have designed them in a way they can implement enhancements like this in the future. Things like non-boxing unions and tagged unions / enhanced enums are still being considered, just not for this version.
- CharlieDigital 6mo agoThis is the general pattern of how the C# team operates, IME. "Never let perfect be the enemy of good" Very much what I've seen from them over the years as they iterate and improve features and propagate it through the platform. AOT as an example; they ship the feature first and then incrementally move first party packages over to support it. Runtime `async` is another example.
- pjmlp 6mo agoIn the meantime I still haven't done any project with nullable references, because the ecosystem has yet to move along. Same applies to ValueTask for async code.
- zigzag312 6mo agoWhich part of the ecosystem is blocking your projects from using nullable references? I find them very helpful, but the projects were all newer or migrated to new SDK.
- pjmlp 6mo agoWhat all projects? It is relatively easy to find corporate libraries, or commercial products that still aren't using it, including Microsoft products still stuck in .NET Framework, or .NET standard 2.0. If you want name shaming of commercial products with modern .NET, here is one, can provide more. https://github.com/episerver/content-templates https://github.com/episerver/content-templates
- functional_dev 6mo agoRight, the default boxes into heap, but unions are different. Some languages pack them as a flat struct (tag + payload, no allocation). Here is visual layout if anyone is interested - https://vectree.io/c/memory-layout-tagging-and-payload-overlapping-of-union-types https://vectree.io/c/memory-layout-tagging-and-payload-overl...
- debugnik 6mo agoThat is not what C# has just added to the language though. These union types so far are just wrappers over an `object` field which gets downcasted. F# offers actual field sharing for value-type (struct) unions by explicitly sharing field names across cases, which is as far as you can push it on the CLR without extra runtime support.