Determine if T as struct is unmanaged at compiling time #3438
Replies: 7 comments
-
Short answer: No. The compiler doesn't care about runtime checks you write against arbitrary type parameters to constrain them further inside of the context of where that type parameter resides. |
Beta Was this translation helpful? Give feedback.
-
This is not an on optimization, it would be more like hinting the compiler that I expect T to be unmanaged so I can use unmanaged datastructures (for example from external libraries). Can the compiler verify my expectation at compiling time? If so it should be safe. a pseudo code that doesn't make much sense, but gives an idea, would be something like:
btw, I can now resolve this problem using reflection. For example I can solve it using MakeGenericMethod, but this wouldn't work in AOT environment, which is the only reason why I wrote about the problem here. |
Beta Was this translation helpful? Give feedback.
-
I already told you: No it can't. For that matter, this usecase is extremely niche. I can't even think of a good reason that you need a different kind of buffer implementation for |
Beta Was this translation helpful? Give feedback.
-
maybe you are right, but with all the unsafe improvements c# went through, using native memory for unmanaged structs may not be that much niche as you think, but I don't disagree in principle. |
Beta Was this translation helpful? Give feedback.
-
Pattern matching on generic constraints #905
unmanaged structs you can block copy as bytes; if they have GC references you can't |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr, we actually use |
Beta Was this translation helpful? Give feedback.
-
I understand I would need a compiler time expression to check if < T > is unmanaged, true, but I would still need a workaround to be able to hint the compiler that T must be now seen as unmanaged when the check passes. I think my point is that the cases where people are forced to use struct, but then want to have different strategies when T is unmanaged, are not as niche as we think with modern c#. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am not a CLR expert, I may be considered a c# expert, but that's it. I just know some bits here and there about how IL and the jitter work but not enough to dive in deep discussion like you do. I just wanted to add my two cents on something that bother me and I hope this is the right place to do so.
In my framework I have several cases where I depend on the execution of static constructor to evaluate some expression that in my opinion could be, sometimes, solved at compile time. Lately I am also finding myself in the need to be able to change the implementation of the code according the characteristic of < T >, for example according if it is a managed or an unmanaged struct, like:
https://bit.ly/2YLSZJo (points to sharplab.io example)
as you can see there, since < T > is marked as a struct and the c# compiler is going to generate specialized code for each < T > anyway, it could check if T is unmanaged and let me return an implementation based on the unmanaged constraint.
maybe the jitter could optimize the branching already in the most recent versions of .net, using IsReferenceOrContainsReferences< T >, but that would not be enough, because I would need also to instruct the compiler that T is allowed to be used in that implementation (something like a compiler time constraint converter).
would such a thing be possible?
Beta Was this translation helpful? Give feedback.
All reactions