-
Notifications
You must be signed in to change notification settings - Fork 10.4k
JSInterop: Enable returning null for a nullable value type #60850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSInterop: Enable returning null for a nullable value type #60850
Conversation
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value. dotnet#30366
Pushed a draft of what I wanted the tests to look like, gotta figure out the test fixture stuff too. |
All reactions
Sorry, something went wrong.
6f7d836
to
2dad3d3
Compare
Ok I kinda figured out how the tests are supposed to work. Just have to install jdk |
All reactions
Sorry, something went wrong.
2dad3d3
to
fe68842
Compare
fe68842
to
0a86864
Compare
Figured it out, tests are green now. |
All reactions
Sorry, something went wrong.
Alternative: use |
All reactions
Sorry, something went wrong.
Ready for review. |
All reactions
Sorry, something went wrong.
@@ -89,7 +89,9 @@ public void SetResult(object tcs, object? result) | |||
// If necessary, attempt a cast | |||
var typedResult = result is T resultT | |||
? resultT | |||
: (T)Convert.ChangeType(result, typeof(T), CultureInfo.InvariantCulture)!; | |||
: result == null && default(T) == null // ChangeType can't convert null to value types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems OK to me.
@oroztocil please review
Sorry, something went wrong.
All reactions
-
👍 1 reaction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why default(T) == null
instead of typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>)
?
GetGenericTypeDefinition
is an intrinsic that will be folded by the compiler, see dotnet/runtime#103528.
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>)
This seems like a better option to me
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hez2010 I didn't know about intrinsics and folding, and honestly still don't entirely understand it. I changed it to check for the generic type.
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and honestly still don't entirely understand it
The git is able to detect that the expression will be constant for a given T and it's able to optimize it entirely when it generates the machine code for a given T. For example, if you have something like T = int
, it's able to know that the condition will always evaluate to false
and it can then use that information to simplify the check (and in this case, unconditionally select one branch or another))
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should go with the alternative implementation that @hez2010 suggested.
Sorry, something went wrong.
All reactions
I'm back from vacation and updated the branch, ready for review. |
All reactions
Sorry, something went wrong.
@Regenhardt thanks for the contribution! |
All reactions
-
❤️ 1 reaction
Sorry, something went wrong.
pavelsavara
javiercn
oroztocil
+1 more reviewer
hez2010
Successfully merging this pull request may close these issues.
jsRuntime.InvokeAsync can't return null as Nullable<Guid>
JSInterop: Enable returning null for a nullable value type
Summary of the changes (Less than 80 chars)
Description
When a result value is null, and the return type is a nullable value type, Convert.ChangeType throws instead of returning the null value, so in case off null this now uses null directly.
Fixes #30366