csharpstandard
csharpstandard copied to clipboard
Null-Coalescing Changes in V8
@BillWagner
This issue interacts with PR https://github.com/dotnet/csharpstandard/pull/609, “Add support for Null-coalescing assignment”.
Background
- Historically, compound-assignment operators have been defined in terms of their non-assignment counterparts. For example, in §11.19.3, “Compound assignment,” the spec says, “An operation of the form x «op»= y is processed by applying binary operator overload resolution (§11.4.5) as if the operation was written x «op» y.” and then goes on to talk about assignment specifics.
- The V4 standard introduced the null-coalescing operator,
??(§11.14 in V7); however, support for??=was not included. - V8 adds support for
??=. Using the MS spec for this feature, Rex created Draft PR 609. - V8 also has an empty placeholder MS spec for allowing unconstrained type parameters with
??and??=. Here’s a simple example:
private static void Mx<T>(T a, T b)
{
Console.WriteLine(a ?? b);
Console.WriteLine(a ??= b);
}
Current Situation
More than a year after he wrote the PR 609 text, Rex recognized that PR and the unconstrained type parameters topic are related, and he is now trying to figure out what the spec should be for allowing unconstrained type parameters. His current thinking is:
- This feature addresses both
??and??=. - Rather than treating the addition of
??=and supporting unconstrained type parameters as separate features, it seems appropriate to combine them into one PR. - The new text re the handling of the unconstrained type parameters should go in the
??operator section, and only if that impacts??=should it be mentioned in the compound-assignment section as well. - In PR 609, the main edits for
??=went into the “Assignment operators” sections; there were no changes to the??operator section itself. In hindsight, we should review the two sections to make sure they work together, and preserve the general notion that the compound-assignment operators be defined in terms of their non-assignment counterparts. - As to the specific changes to support unconstrained type parameters, it seems this would (mostly?) involve the addition of a new, first, step to the bullet list in §11.14, “The null coalescing operator.”
Bottom line
Rex needs someone to
- Write the words for the new step to support unconstrained type parameters, and to see if they need to overflow into the compound-assignment section.
- Review the edits in PR 609 and reconcile them with these new words; that is, to unify the two sets of edits.
@RexJaeschke
Based on some testing using unconstrained type parameters with ??:
- Both arguments must be typed with the same unconstrained type parameter, say
T - If
Tat runtime is a reference or nullable value type then the semantics follow??with reference or nullable value type arguments respectively - If
Tat runtime is a non-nullable value type thena ?? bisa -
Tcannot be unmanaged at runtime as that requires a constraint...
Can you wordsmith from here?
Make sure it allows
public class C {
public void M<T1, T2>(T1 a, ref T2 b) where T1 : T2 {
b ??= a;
}
}