Null 결합 할당null coalescing assignment
- [x] 제안 됨[x] Proposed
- [x] 프로토타입: 완료 됨[x] Prototype: Completed
- [x] 구현: 완료 됨[x] Implementation: Completed
- [x] 사양: 아래[x] Specification: Below
요약Summary
Null 인 경우 변수에 값을 할당 하는 일반적인 코딩 패턴을 간소화 합니다.Simplifies a common coding pattern where a variable is assigned a value if it is null.
이 제안의 일부로,의 형식 요구 사항을 완화 하 여 해당 ??
형식이 제약 조건이 없는 형식 매개 변수를 왼쪽에서 사용 하는 식을 허용 합니다.As part of this proposal, we will also loosen the type requirements on ??
to allow an expression whose type is an unconstrained type parameter to be used on the left-hand side.
동기Motivation
일반적으로 폼의 코드를 볼 수 있습니다.It is common to see code of the form
if (variable == null)
{
variable = expression;
}
이 제안은이 기능을 수행 하는 언어에 오버 로드할 수 없는 이항 연산자를 추가 합니다.This proposal adds a non-overloadable binary operator to the language that performs this function.
이 기능에 대 한 별도의 커뮤니티 요청이 8 개 이상 있습니다.There have been at least eight separate community requests for this feature.
자세한 디자인Detailed design
새 형식의 대입 연산자를 추가 합니다.We add a new form of assignment operator
assignment_operator
: '??='
;
이는 왼쪽이 null이 아닌 경우 할당을 elide 한다는 점을 제외 하 고 복합 할당 연산자에 대 한 기존 의미 체계 규칙을 따릅니다.Which follows the existing semantic rules for compound assignment operators, except that we elide the assignment if the left-hand side is non-null. 이 기능에 대 한 규칙은 다음과 같습니다.The rules for this feature are as follows.
지정 된입니다. 여기서는의 형식이 고는가 a ??= b
A
a
B
b
A0
A
nullable 값 형식인 경우의 기본 형식입니다 A
.Given a ??= b
, where A
is the type of a
, B
is the type of b
, and A0
is the underlying type of A
if A
is a nullable value type:
- 가
A
없거나 null을 허용 하지 않는 값 형식인 경우 컴파일 타임 오류가 발생 합니다.IfA
does not exist or is a non-nullable value type, a compile-time error occurs. B
가 또는 (있는 경우)로 암시적으로 변환할 수 없는 경우A
A0
A0
컴파일 타임 오류가 발생 합니다.IfB
is not implicitly convertible toA
orA0
(ifA0
exists), a compile-time error occurs.- 이
A0
존재 하 고B
가로 암시적으로 변환할 수A0
있으며B
동적이 아닌 경우의 형식은a ??= b
입니다A0
.IfA0
exists andB
is implicitly convertible toA0
, andB
is not dynamic, then the type ofa ??= b
isA0
.a ??= b
는 런타임에 다음과 같이 계산 됩니다.a ??= b
is evaluated at runtime as:
제외 하 고var tmp = a.GetValueOrDefault(); if (!a.HasValue) { tmp = b; a = tmp; } tmp
a
한 번만 평가 됩니다.Except thata
is only evaluated once. - 그렇지 않으면의 형식은
a ??= b
A
입니다.Otherwise, the type ofa ??= b
isA
.a ??= b
는a ?? (a = b)
a
한 번만 계산 된다는 점을 제외 하 고는 런타임에로 평가 됩니다.a ??= b
is evaluated at runtime asa ?? (a = b)
, except thata
is only evaluated once.
의 형식 요구 사항에 대 한 완화을 위해 ??
현재 상태를 나타내는 사양을 업데이트 합니다 a ?? b
A
. 여기서은의 형식입니다 a
.For the relaxation of the type requirements of ??
, we update the spec where it currently states that, given a ?? b
, where A
is the type of a
:
- 가 있고 nullable 형식 또는 참조 형식이 아닌 경우 컴파일 타임 오류가 발생 합니다.If A exists and is not a nullable type or a reference type, a compile-time error occurs.
이 요구 사항은 다음과 같이 완화 됩니다.We relax this requirement to:
- 이 존재 하 고 null을 허용 하지 않는 값 형식인 경우 컴파일 타임 오류가 발생 합니다.If A exists and is a non-nullable value type, a compile-time error occurs.
이렇게 하면 제약 조건이 없는 형식 매개 변수 T가 null이 고 nullable 형식이 아니므로 참조 형식이 아니므로 null 병합 연산자가 제한 되지 않은 형식 매개 변수에 대해 작업을 수행할 수 있습니다.This allows the null coalescing operator to work on unconstrained type parameters, as the unconstrained type parameter T exists, is not a nullable type, and is not a reference type.
단점Drawbacks
모든 언어 기능을 사용 하는 것과 마찬가지로,이 기능을 활용 하는 c # 프로그램의 본문에 제공 되는 추가 명확성에서 언어에 대 한 추가 복잡성이 다시 지불 되는지 여부를 확인 해야 합니다.As with any language feature, we must question whether the additional complexity to the language is repaid in the additional clarity offered to the body of C# programs that would benefit from the feature.
대안Alternatives
프로그래머는, 또는을 직접 작성할 수 있습니다 (x = x ?? y)
if (x == null) x = y;
x ?? (x = y)
.The programmer can write (x = x ?? y)
, if (x == null) x = y;
, or x ?? (x = y)
by hand.
확인 되지 않은 질문Unresolved questions
- []에는 LDM 검토가 필요 합니다.[ ] Requires LDM review
- []도
&&=
및 연산자를 지원||=
하나요?[ ] Should we also support&&=
and||=
operators?
디자인 회의Design meetings
없음None.