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

Api renewals #5

Merged
merged 9 commits into from
Dec 19, 2016
6 changes: 4 additions & 2 deletions src/ContextFreeTasks.Shared/ContextFreeTask.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using static System.Threading.Tasks.Task;

namespace ContextFreeTasks
{
[AsyncMethodBuilder(typeof(AsyncContextFreeTaskMethodBuilder))]
public struct ContextFreeTask
{
public Task Task { get; }
public ContextFreeTask(Task t) => Task = t;
private readonly Task _task;
public Task Task => _task ?? FromResult(default(object));
internal ContextFreeTask(Task t) => _task = t;
Copy link
Owner

@ufcpp ufcpp Dec 16, 2016

Choose a reason for hiding this comment

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

So far, await on null should throw an Exception. For the purpose of null-safety, the constructor should throw an exception if t is null and _task should not be null. Of course, default(ContextFreeTask) makes _task be null, but, it is misuse.

I hope the following proposals:

dotnet/roslyn#7171
dotnet/roslyn#15108

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I concerned about default(ContextFreeTask). if provides struct as public, we must consider the default.
for example, ValueTask created with default keyword, it is same as FromResult(default(T)).
the constructor of ContextFreeTask is hid to internal. so ONLY default(ContextFreeTask) makes _task be null. it's very rare case.

public ContextFreeTaskAwaiter GetAwaiter() => new ContextFreeTaskAwaiter(Task);
}
}
6 changes: 4 additions & 2 deletions src/ContextFreeTasks.Shared/ContextFreeTask_T.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using static System.Threading.Tasks.Task;

namespace ContextFreeTasks
{
[AsyncMethodBuilder(typeof(AsyncContextFreeTaskMethodBuilder<>))]
public struct ContextFreeTask<T>
{
public Task<T> Task { get; }
public ContextFreeTask(Task<T> t) => Task = t;
private readonly Task<T> _task;
public Task<T> Task => _task ?? FromResult(default(T));
internal ContextFreeTask(Task<T> t) => _task = t;
public ContextFreeTaskAwaiter<T> GetAwaiter() => new ContextFreeTaskAwaiter<T>(Task);
}
}