Repository navigation
Description
I propose we add a valueTask { } computation expression to FSharp.Core: a resumable-code builder over AsyncValueTaskMethodBuilder<'T> / AsyncValueTaskMethodBuilder, producing ValueTask<'T> and ValueTask, shaped exactly like the existing task { } and available on the netstandard2.1 and net* target frameworks.
This re-raises #1244, closed as Not Planned on 2024-06-12, together with its implementation dotnet/fsharp#14755. I want to be precise about what has and has not changed since, because #1244 was closed for two distinct reasons.
The dependency objection is unchanged, and this proposal does not ask you to move on it. FSharp.Core still takes no dependency on System.Threading.Tasks.Extensions, so AsyncValueTaskMethodBuilder remains unavailable on netstandard2.0 and the builder cannot ship there. That is accepted here as a constraint, not contested.
The closing reason was the second one — "until we have a better story for such big differences in the public surface area for different targets". What has changed there is precedent rather than written policy, and it is worth weighing honestly:
- FSharp.Core 11 shipped the
ValueTaskmodule (result,map,bind,ignore,catchWith,catch,empty,ofTask) andTask.ofValueTask(#1466, PR #19844),Async.Awaitoverloads forValueTask/ValueTask<'T>(#840, PR #19785) andAsync.StartTaskImmediateoverloads for the same (#1284, PR #20258) — all under#if NETSTANDARD2_1 || NET, all with no new dependency, all merged in August 2026. - In review, the resulting TFM divergence was treated as routine: the one comment it drew was a request to regenerate the four
FSharp.Core.SurfaceArea.netstandard2{0,1}.{debug,release}.bslbaselines. The baselines now record the split as a reviewed artifact — noValueTaskentries onnetstandard2.0, fourteen onnetstandard2.1, eighteen onnetcore. - But nobody wrote the story down. There is no documented policy in
docs/, infsharp-core-notes.mdor in an issue that a proposal like this one can point at and say "this is covered". If the team's position is that the policy has to be written before another TFM-conditional API lands, that is a fair answer, and I would rather help draft it than argue the point.
What is genuinely different about this particular API, and what was not said in #1244:
- On
netstandard2.0FSharp.Core cannot so much as nameValueTask. Nothing regresses and the divergence does not widen in a new direction — it lands in the same place the existingValueTasksurface already lives. - Consuming a
ValueTaskintask { }already works on every target framework, becauseLowPriority.Bind<^TaskLike, …>is structural SRTP overGetAwaiterand is resolved in the consumer's compilation, not in FSharp.Core's. Only producing one needs the builder. So what is missing onnetstandard2.0is a single builder value, not — in the words of Support for ValueTask state machine computation expression #1244's first comment — a "whole chunk of API". The asymmetry is the real argument: F# already awaitsValueTaskeverywhere and cannot return one anywhere.
The existing way of approaching this problem in F# is to write task { … } and wrap the result:
member this.ReadAsync(buffer) : ValueTask<int> =
task {
if cached.Length > 0 then return cached.Length // synchronous path, still allocates a Task
else return! this.SlowPath(buffer)
}
|> ValueTask<int>which allocates on every call the Task that ValueTask exists to avoid, defeating the point of returning one. The alternative is a third-party builder — IcedTasks has valueTask, poolingValueTask and cancellableValueTask, and was cited in #1244's own closure as the reason to decline.
Pros and Cons
The advantages of making this adjustment to F# are
- A computation that completes synchronously allocates nothing. That is the entire reason
ValueTaskexists, and F# currently cannot express it. - Implementing a
ValueTask-returning interface member is routine in modern .NET —IAsyncDisposable.DisposeAsync,IAsyncEnumerator.MoveNextAsync,IValueTaskSource<'T>, and large parts of ASP.NET Core, Roslyn, ServiceHub and the VS SDK. Today every one of those forces the wrap above, so F# code implementing a hot-path .NET interface is structurally slower than the C# it is talking to. - Parity with C#
async ValueTask/async ValueTask<T>, available since C# 7. - It completes a story FSharp.Core has now half-shipped: the
ValueTaskmodule,Async.AwaitandAsync.StartTaskImmediateall handleValueTask, but nothing in the language produces one. - An implementation already exists: PR #14755 by @thinkbeforecoding, closed only because the suggestion was.
The disadvantages of making this adjustment to F# are
- The public surface differs between
netstandard2.0and the other target frameworks. Already true of everyValueTaskAPI in FSharp.Core 11 and already recorded per TFM in the surface-area baselines — but it is one more thing anetstandard2.0consumer will not find, and, unlike a missing function, a missing builder is discovered as a syntax-level failure rather than a missing name. - A second builder largely parallel to
TaskBuilder, with the attendant risk of the two drifting. The resumable-code scaffolding should be shared rather than copied. ValueTaskmay be awaited only once and must not be awaited after.AsTask(). A builder makes it easy to produce them, and therefore easy to misuse them. This needs to be prominent in the documentation; C# has the same hazard and documents it the same way.- Open design question: whether to also expose
poolingValueTask { }overPoolingAsyncValueTaskMethodBuilder(net6+), which trades pooled state machines for an allocation on the asynchronous path. It narrows the surface further by TFM, so it is probably a follow-up rather than part of this.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M — PR #14755 is an existing implementation needing a rebase, a surface-area baseline update for netstandard2.1/netcore only, and tests.
Related suggestions:
- #1244 — the same request, closed Not Planned; this re-raises it
- #581 — native
task { }, the template this follows - #1466, #1284, #840 — the shipped
ValueTaskAPIs discussed above - #866 —
DisposeAsyncinasync, which needs the sameValueTaskplumbing - #1459 —
syncTask, another proposed builder in the same family
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
- This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
- This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
- This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
- I have searched both open and closed suggestions on this site and believe this is not a duplicate — Support for ValueTask state machine computation expression #1244 is the same request, closed for a reason I am asking you to re-weigh rather than one I claim has evaporated. Happy for this to be folded back into Support for ValueTask state machine computation expression #1244 if the maintainers would rather reopen it than track a second thread.
Please tick all that apply:
- This is not a breaking change to the F# language design
- I or my company would be willing to help implement and/or test this
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
Activity
T-Gro commentedon Sep 21, 2026
As of now (net11), I see more value in focusing on runtime async. Many uses of
Taskget optimized away by the runtime and there is no additional allocation anymore.More specifically, I would not want to introduce a new
F#-resumable-codebuilder now.We can certainly leave this open and wait for some benchmarks around scenarios with
runtime asyncthat show a clear benefit. The F# recognized primitives for runtime async work withValueTaskas well.davidglassborow commentedon Sep 21, 2026
@T-Gro is
runtime asyncsupport in F# being tracked somewhere ? Presumably F# Task code won't currently take advantage of the new opt in .net11 features ?vzarytovskii commentedon Sep 21, 2026
One question to ask when theres a suggestion to add something to fslib - why does it have to be in core library and can't be something like
IcedTasks?xperiandri commentedon Sep 21, 2026
Should be in core lib, my mistake
vzarytovskii commentedon Sep 21, 2026
Yeah, I get that. I'm just curious about reasoning on why.
T-Gro commentedon Sep 21, 2026
@davidglassborow : net11 F# compiler comes equipped with the necessary compiler-recognized primitives and we do have a prototype of the builder, which people can copy to their projects or host in a 3rd party package. It is not replacing the built-in
task{}for now in any way.davidglassborow commentedon Sep 21, 2026
See dotnet/fsharp#20235 for more details
charlesroddie commentedon Sep 22, 2026
Wow it's huge that F# is getting runtime async. Is the syntax for this ironed out? The PR doc https://github.com/dotnet/fsharp/pull/20235/changes#diff-1016087f2b2c5383cc3f16d68b905f0e24f87acad94712072b7730f089774f4dR252 suggests that enabling the preview doesn't immediately use the feature in existing
async/taskbut instead is the currently scaffolding on which users can buildasync/taskreplacements?davidglassborow commentedon Sep 23, 2026
That's my reading of it, that new bulders outside of FSharp.Core can be written to allow runtime async, starting from the prototype
T-Gro commentedon Sep 23, 2026
@charlesroddie :
Yes, the work which has landed is just enablement for it, not changing existing builder.
Eventually, I would want to to just fuse with the
task{}builder. However, for an interims period, we might need something like #1473 to let users selectively pick behavior.majocha commentedon Sep 26, 2026
There's a toy project FSharp.Control.Async2 implementing a cold cancellable
async2builder and theasyncSeq2IAsyncEnumerable. async2 aims to be async drop in replacement and asyncSeq2 implements functionality of both asyncSeq and taskSeq. Respectable test suites are ported. Missing part for now is MailBoxProcessor. We should definitely add a RFC, but things went fast, sorry for that.