Skip to content
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

Add ValueTask factory members #37507

Merged
merged 1 commit into from
Jun 6, 2020
Merged

Conversation

stephentoub
Copy link
Member

Fixes #27960
cc: @tarekgh

@ghost
Copy link

ghost commented Jun 5, 2020

Tagging subscribers to this area: @tarekgh
Notify danmosemsft if you want to be subscribed.

@Dotnet-GitSync-Bot
Copy link
Collaborator

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@stephentoub stephentoub changed the title Add ValueTask.factory members Add ValueTask factory members Jun 5, 2020
@tarekgh
Copy link
Member

tarekgh commented Jun 5, 2020

@stephentoub

System\IO\Compression\enc\BrotliStream.Compress.cs(79,20): error CS0173: (NETCORE_ENGINEERING_TELEMETRY=Build) Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Threading.Tasks.ValueTask<int>' and 'System.Threading.Tasks.ValueTask'

@stephentoub
Copy link
Member Author

Thanks. Will fix.

@stephentoub stephentoub merged commit 4b339d8 into dotnet:master Jun 6, 2020
@stephentoub stephentoub deleted the valuetaskfrom branch June 6, 2020 21:54
@@ -114,6 +111,40 @@ private ValueTask(object? obj, short token, bool continueOnCapturedContext)
_continueOnCapturedContext = continueOnCapturedContext;
}

/// <summary>Gets a task that has already completed successfully.</summary>
public static ValueTask CompletedTask => default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This member feels confusing from an API standpoint. Per https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask?view=net-5.0:

The following operations should never be performed on a ValueTask instance:

  • Awaiting the instance multiple times.

Although it's actually legal, it feels like using ValueTask.CompletedTask leads immediately to violating the above "should never" guideline. If I return this "instance" from my own method, won't my method's callers be awaiting the "same" instance multiple times and violating the rule? (Saying it's a by-value copy and they're actually different instances doesn't resolve the problem, as copying a ValueTask doesn't provide an exception to the current set of rules.)

If it's argued that this is a special case and can be treated differently, that just makes the ValueTask rules more difficult to comply with, as they can no longer be applied consistently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair question, but that assumes it's returning the same instance each time you invoke the getter. Think of it as a member you're invoking and it's giving you back an already completed task you can use once. It's not special in that way. I can invoke Stream.ReadAsync and use the returned ValueTask once. I can invoke ValueTask.FromException and use the returned ValueTask once. ValueTask.CompletedTask is the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the response; that's a fair interpretation.

I was going to argue that it is then inconsistent with Task.CompletedTask, which returns the same instance for efficiency, but then I read the docs and saw that "Repeated attempts to retrieve this property value may not always return the same instance.", so TIL.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API Proposal: Add to ValueTask: CompletedTask, FromResult and FromDefault
4 participants