csharpstandard icon indicating copy to clipboard operation
csharpstandard copied to clipboard

Null-Coalescing Changes in V8

Open RexJaeschke opened this issue 2 years ago • 2 comments

@BillWagner

This issue interacts with PR https://github.com/dotnet/csharpstandard/pull/609, “Add support for Null-coalescing assignment”.

Background

  1. 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.
  2. The V4 standard introduced the null-coalescing operator, ??11.14 in V7); however, support for ??= was not included.
  3. V8 adds support for ??=. Using the MS spec for this feature, Rex created Draft PR 609.
  4. 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:

  1. This feature addresses both ?? and ??=.
  2. Rather than treating the addition of ??= and supporting unconstrained type parameters as separate features, it seems appropriate to combine them into one PR.
  3. 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.
  4. 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.
  5. 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

  1. 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.
  2. Review the edits in PR 609 and reconcile them with these new words; that is, to unify the two sets of edits.

RexJaeschke avatar Feb 15 '23 14:02 RexJaeschke

@RexJaeschke

Based on some testing using unconstrained type parameters with ??:

  • Both arguments must be typed with the same unconstrained type parameter, say T
  • If T at runtime is a reference or nullable value type then the semantics follow ?? with reference or nullable value type arguments respectively
  • If T at runtime is a non-nullable value type then a ?? b is a
  • T cannot be unmanaged at runtime as that requires a constraint...

Can you wordsmith from here?

Nigel-Ecma avatar Feb 24 '23 05:02 Nigel-Ecma

Make sure it allows

public class C {
    public void M<T1, T2>(T1 a, ref T2 b) where T1 : T2 {
        b ??= a;
    }
}

KalleOlaviNiemitalo avatar Feb 24 '23 09:02 KalleOlaviNiemitalo