Replies: 8 comments
-
The problem with adding more generic constraints is that it would either need extending CIL to support them, or some workaround like custom attributes, both of which would in this case also need to be able to be handled by other languages and compilers, to ensure type and memory safety. But it doesn't mean it is impossible, F# already has that, albeit stored in the assembly resources understood only by F#. |
Beta Was this translation helpful? Give feedback.
-
Why is this needed as a language feature beyond If it’s as a constant, what happens when the struct is in a different assembly? |
Beta Was this translation helpful? Give feedback.
-
For non-primitive types, |
Beta Was this translation helpful? Give feedback.
-
WE NEED BLITTABLE GENERIC CONSTRAINT. |
Beta Was this translation helpful? Give feedback.
-
@dangi12012 When in Rome, do as the Romans do. Have you seen other participants in csharplang use all-caps and bold for effect? |
Beta Was this translation helpful? Give feedback.
-
@dangi12012, the |
Beta Was this translation helpful? Give feedback.
-
@IllidanS4 |
Beta Was this translation helpful? Give feedback.
-
See also #1508 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@whoisj commented on Sun May 31 2015
I took often find myself writing long lists of methods (like below) where I'm reading the bits of stack values for things like hashing, serialization, encryption, etc. because I'm unable to acquire
sizeof(T)
evenwhen T : struct
which I I find fairly frustrating.Ideally the
sizeof(...)
operator would work on generics so long as the type being measured is guaranteed to be a value type (ala struct).This should be perfectly legal code:
Then I would only need a single method and not a dozen.
Support for
T*
is #3210@whoisj commented on Tue Jun 02 2015
Updated based on the conversation happening in #3210
@damageboy commented on Thu Apr 14 2016
@whoisj while I would obviously agree with you that this is very handy and should be supported directly in c#, isn't something along the lines of http://stackoverflow.com/questions/18167216/size-of-generic-structure a temporary workaround for you? (With very little performance overhead...)
@whoisj commented on Thu Apr 14 2016
The solution presented on SO seems reasonable, but I'm not certain about the runtime costs associated with passing value through the marshaler to get its size. Still, it seems to me to be a work-around for a missing feature, especially when the underlying IL supports the operation and it is C# which imposes the limitation.
@axel-habermaier commented on Thu Aug 25 2016
There is also DotNetCross.Memory.Unsafe that exposes some IL instructions as regular .NET methods that are unavailable in C#. The library is implemented using IL instead of C#. For instance:
In my opinion, however, most of the stuff exposed by the library should be available directly in C#.
@ghord commented on Thu Aug 25 2016
blittable
constraint is such a nice way to solve most of the issues with generic pointers, yet everyone keeps insisting on using hacks likeUnsafe.Size<T>()
. Just look how clean following code is:@jcouv commented on Sat Oct 21 2017
Moving language design discussion to csharplang. Thanks
Beta Was this translation helpful? Give feedback.
All reactions