Repository navigation
parser: support nullable value types in operators - #30
Merged
Merged
Conversation
Operators accepted operands of a nullable value type only when both operands had the same type, as in x + y for two int? values. Mixed operands were rejected: for an int? x, x ?? 5, x + 1, x > 1, and Flag ? x : 1 all failed, as did x == DayOfWeek.Friday for a DayOfWeek? x and x > Date for a DateTime? x. The unary '~' did not promote a byte? operand, so ~x for x = 3 returned 252 as byte? instead of -4 as int?. Follow the C# rules for nullable value types: - Add the implicit conversions from S and S? to T? for each identity or numeric conversion from S to T. The conditional operator uses them to find the common type of its branches. - Determine the result type of '??' in the order of the specification: A0, A, then B. For example, x ?? 1 is an int, x ?? 1L is a long, and x ?? y is a long? for a long? y. - Lift the unary and binary operators, including shifts, enum operators, and user-defined operators such as DateTime subtraction: select the operator by the underlying types of the operands and convert the operands to the nullable forms of its operand types. - Give the null literal the type of the nullable operand on the other side, as in x == null or x ?? null.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Operators accepted operands of a nullable value type only when both operands had the same type, as in
x + yfor twoint?values. Mixed operands were rejected: for anint? x,x ?? 5,x + 1,x > 1, andFlag ? x : 1all failed, as didx == DayOfWeek.Fridayfor aDayOfWeek? xandx > Datefor aDateTime? x. The unary~did not promote abyte?operand, so~xforx = 3returned252asbyte?instead of-4asint?.This PR follows the C# rules for nullable value types:
SandS?toT?for each identity or numeric conversion fromStoT. The conditional operator uses them to find the common type of its branches.??in the order of the specification:A₀,A, thenB. For example,x ?? 1is anint,x ?? 1Lis along, andx ?? yis along?for along? y.DateTimesubtraction: the operator is selected by the underlying types of the operands, and the operands are converted to the nullable forms of its operand types.x == nullorx ?? null.The unary
~on a nullable operand of a type smaller thanint, such asbyte?, now returnsint?. Other expressions that parsed before keep their result type and value.??with a left operand of a reference type is not changed:name ?? objfor astringand anobjectis still rejected, though C# accepts it. Constants are still converted by their type, sox + 1is rejected for aulong? x, as it is for aulong.Lifted enum operators follow the existing rules for enums. For example,
x - DayOfWeek.Mondayfor aDayOfWeek? xreturns aDayOfWeek?, as it does for aDayOfWeek.