-
Notifications
You must be signed in to change notification settings - Fork 167
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
Why not store the instance in an object
field instead of a bunch of fields?
#158
Comments
Because you'd be boxing what are value types etc, and generally wasting space, causing extra allocations etc |
It's my understanding that any The big win would be when there are multiple value types. Consider a Another thought: there could be an size optimization for value type readonly struct OneOf<T0, T1>
{
private readonly Object? value;
private readonly UInt64 smallUnmanagedValueTypeRawBytes;
private readonly Int32 index;
... |
Apologies for coming over all conclusive on this - I was merely trying to give you an answer as to why I would expect the proposed mechanism to not be a win. Please note I'm not the maintainer and/or a contributor, and my only intent is to move the ball forward in terms of moving from "why didnt you do this instead" onto either "ah, I see now" or "actually, look - it works this much better , see the benchmark.net test" I can't say I've particularly thought about in depth. |
No problem. Thank you for your responses and help so far :) |
It's been years since I made the decision, but my thinking was
1. Boxing is very slow, use generics
2. Casting is a bit slow, use generics
3. Memory is relatively ample these days, and references to heap and most
structs aren't massive so it's ok to use a lot of them
4. F# struct unions are implemented this way (I vaguely remember digging
around in ILSpy to figure this out). Seems like a relevant discussion
dotnet/fsharp#9368
…On Fri, 18 Aug 2023, 17:37 Nick Strupat, ***@***.***> wrote:
No problem. Thank you for your responses and help so far :)
—
Reply to this email directly, view it on GitHub
<#158 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACDJ6VPEB5RMOL6C43E35LXV6K4NANCNFSM6AAAAAA3UYINRM>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
if you’re happy with boxing and casting, you could just use an object and a switch expression, in vanilla C#?
|
Here is a new proposal for the C# language to implement unions with an object field: dotnet/csharplang#7544 |
Like this:
The text was updated successfully, but these errors were encountered: