Replies: 1 comment 7 replies
-
Additionally, |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Motivation
Nullable<T>
where T : unmanaged
is effectively an unmanaged type (being a struct that contains fields of unmanaged types only). Unfortunately, it doesn't satisfy thestruct
/unmanaged
constraints, which causes bloated APIs like:This becomes exponentially worse the more parameters there are:
To avoid an explosion of overloads, the library author may decide to leave
T
unconstrained and instead throw an exception on misuse:This approach detects issues at runtime with zero overhead, but the method signature no longer states the actual requirements and we lose compile time checks.
It's too late to change the semantics of the
unmanaged
constraint, but why not add a relaxed form to allow the following?Proposal
Define an
unmanaged?
constraint which accepts both unmanagedT
andT?
. It still implies thenew()
constraint, but notstruct
.Alternative Designs
Register a code analyzer to constrain the parameters:
Beta Was this translation helpful? Give feedback.
All reactions