Add where T : struct?
generic type constraint
#7878
Unanswered
AlexRadch
asked this question in
Language Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It is impossible to write one generic method for non-nullable value type and nullable value type.
Many generic methods do not support a nullable value type due to the lack of such a feature.
For example, MemoryMarshal.Сast does not support nullable values
To support MemoryMarshal.Cast method for a nullable value type, you need to create 3 more variants of it with the same code.
I suggest adding
where T : struct?
generic type constraint and then such a method with a new constraint will support all 4 options.The interface can only be requested after checking that the value exists:
If the
TFrom
is a non-nullable value type, then the checks are always successful, and a copy of the structure is created.Beta Was this translation helpful? Give feedback.
All reactions