Provide a mechanism for declaring c-style anonymous unions and structs. #687
Replies: 14 comments 3 replies
-
In the above examples...
[StructLayout(LayoutKind.Explicit)]
public struct LARGE_INTEGER
{
[FieldOffset(0)]
public double Value;
[FieldOffset(0)]
public long RawBits;
}
[StructLayout(LayoutKind.Explicit)]
public struct LARGE_INTEGER
{
[FieldOffset(0)]
public <SpecialCompilerTypeName> <SpecialCompilerFieldName>;
[FieldOffset(0)]
public long QuadPart;
[StructLayout(LayoutKind.Sequental)]
public struct <SpecialCompilerTypeName>
{
public uint LowPart;
public int HighPart;
}
}
[StructLayout(LayoutKind.Sequental)]
public struct LARGE_INTEGER
{
public DXGI_FORMAT Format;
public D3D12_RTV_DIMENSION ViewDimension;
public <SpecialCompilerTypeName> <SpecialCompilerFieldName>;
[StructLayout(LayoutKind.Explicit)]
public struct <SpecialCompilerTypeName>
{
[FieldOffset(0)]
public D3D12_BUFFER_RTV Buffer;
[FieldOffset(0)]
public D3D12_TEX1D_RTV Texture1D;
[FieldOffset(0)]
public D3D12_TEX1D_ARRAY_RTV Texture1DArray;
[FieldOffset(0)]
public D3D12_TEX2D_RTV Texture2D;
[FieldOffset(0)]
public D3D12_TEX2D_ARRAY_RTV Texture2DArray;
[FieldOffset(0)]
public D3D12_TEX2DMS_RTV Texture2DMS;
[FieldOffset(0)]
public D3D12_TEX2DMS_ARRAY_RTV Texture2DMSArray;
[FieldOffset(0)]
public D3D12_TEX3D_RTV Texture3D;
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm not sure these use cases warrant new keywords. Seems like there are already enough features to declare these interop types. Yes, it's not super concise but it's interop - very few types in most apps would use this. |
Beta Was this translation helpful? Give feedback.
-
I think C# is taking a direction where these type of structures may become more commonplace (especially as we provide more safe ways to deal with memory, such as Span, ref readonly, etc). These type of structures are used in interop for certain, but they are also very applicable to high-performance and low-memory code, such as code which framework authors may use. |
Beta Was this translation helpful? Give feedback.
-
At the very least, I think |
Beta Was this translation helpful? Give feedback.
-
I'm not sure that inclusion of a new keyword is realistic, especially for special-case code. Maybe there could be a special attributes [Union] and [Anonymous] that would do the same thing but help with verbostiy? |
Beta Was this translation helpful? Give feedback.
-
I think this is one of the cases where new 'keywords' is very realistic. The keywords would be considered contextual and would have similar semantics to 'partial' (they can only appear directly before the |
Beta Was this translation helpful? Give feedback.
-
I like the new keywords. |
Beta Was this translation helpful? Give feedback.
-
I want to add an important scenario in which C# is used that is partly similar with interop: Cosmos that is writing an operating system using C# itself! Anything that permits us to talk better with the hardware without the need to use unsafe code or X# (assembler) is welcome. In the "baroque" X86 architecture surely there will be cases in which we will need something similar to C unions... |
Beta Was this translation helpful? Give feedback.
-
The problem that I have with this is the same problem I have with exposing any autogenerated type, that name becomes a part of the contract. Even if that doesn't matter in interop scenarios it certainly matters in non-interop scenarios and I don't see a way to forcibly prevent the latter short of forcing these types to be nested and private. The method through which that name is generated would end up having to be very strictly defined and embedded in the C# specification to allow for consuming code to survive a recompilation of a dependency as well for other compilers to predictably use the type. |
Beta Was this translation helpful? Give feedback.
-
That and this interop "concern" has been around since C# 1.0 and it doesn't seem to have posed a real problem for people writing said interop code. I've translated a ton of Win32 API myself and don't believe I've ever found the process that onerous. It's always nice to have to type less, but at this point is there that much more interop code that needs to be written? Even for anything new it's generally once-and-done and you can find the types/signatures somewhere to copy&paste or reference in some glue assembly. |
Beta Was this translation helpful? Give feedback.
-
These keywords can be contextual so there'll be no way of it causing issues. From what I understand, it's already possible to get this effect, just in a very verbose manner. And, seeing as C# is very big on syntactic sugar and making us need to type less, I think this is a very realistic idea. You might think that there's no chance because people have wanted this for ages and still haven't got it, but I think now there is a good chance. The latest C# updates have included new ways to use memory management in a much more safer way. I believe it was Made himself that said he wanted to give C# more low level features |
Beta Was this translation helpful? Give feedback.
-
i hope got this in C# |
Beta Was this translation helpful? Give feedback.
-
It strikes me that this might possibly be achieved with an additional [StructLayout(LayoutKind.Union)]
public struct IEEE_754_DOUBLE
{
public double Value;
public long RawBits;
} ? |
Beta Was this translation helpful? Give feedback.
-
Regarding:
public union struct LARGE_INTEGER
{
public anonymous struct
{
public uint LowPart;
public int HighPart;
}
public long QuadPart;
} If changes were made to Also how could one even have multiple anonymous structs when they have no name? |
Beta Was this translation helpful? Give feedback.
-
Rationale
When writing interop code (and even occasionally in normal managed code), it is often necessary to declare
union
types and inline anonymous structs.C# currently allows users to declare
union
types by decorating a struct with the[StructLayout(LayoutKind.Explicit)]
attribute and then decorating each field with the[FieldOffset(int)]
attribute.However, the
unions
are a special type of explicit-field layout in that all fields exist at the same address.These unions may additionally be part of a larger struct and may be 'anonymous'.
Proposal
C# should support a new
union
keyword which explicitly lays all fields out to the same address (FieldOffset: 0).C# should additionally support a new
anonymous
keyword which indicates the inline type is explicitly unnamed.Examples
Standalone union
Standalone union with anonymous struct
Struct with anonymous union
Other Thoughts
Like in C/C++, anonymous structs/unions should be accessible in the parent type (i.e.
LARGE_INTEGER.LowPart
rather thanLARGE_INTEGER.<anonymous_struct_name>.LowPart)
).The developers who will use this likely fall into the same category as those that would use
blittable
andbit field layouts
.Beta Was this translation helpful? Give feedback.
All reactions