Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
F Sharp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Asynchronous programming=== F# supports [[asynchronous programming]] through ''asynchronous workflows''.<ref name="aw"/> An asynchronous workflow is defined as a sequence of commands inside an <code>async{ ... }</code>, as in <syntaxhighlight lang="fsharp"> let asynctask = async { let req = WebRequest.Create(url) let! response = req.GetResponseAsync() use stream = response.GetResponseStream() use streamreader = new System.IO.StreamReader(stream) return streamreader.ReadToEnd() } </syntaxhighlight> The <code>let!</code> indicates that the expression on the right (getting the response) should be done asynchronously but the flow should only continue when the result is available. In other words, from the point of view of the code block, it's as if getting the response is a blocking call, whereas from the point of view of the system, the thread won't be blocked and may be used to process other flows until the result needed for this one becomes available. The async block may be invoked using the <code>Async.RunSynchronously</code> function. Multiple async blocks can be executed in parallel using the <code>Async.Parallel</code> function that takes a list of <code>async</code> objects (in the example, <code>asynctask</code> is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using <code>Async.RunSynchronously</code>.<ref name="aw">{{cite web |url=http://blogs.msdn.com/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx |title=Introducing F# Asynchronous Workflows |access-date=2007-12-14}}</ref> [[Inversion of control]] in F# follows this pattern.<ref name="aw"/> Since version 6.0, F# supports creating, consuming and returning .NET tasks directly. <ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/task-expressions | title=Task Expressions| date=19 April 2022|access-date=2023-01-15}}</ref> <syntaxhighlight lang="fsharp"> open System.Net.Http let fetchUrlAsync (url:string) = // string -> Task<string> task { use client = new HttpClient() let! response = client.GetAsync(url) let! content = response.Content.ReadAsStringAsync() do! Task.Delay 500 return content } // Usage let fetchPrint() = let task = task { let! data = fetchUrlAsync "https://example.com" printfn $"{data}" } task.Wait() </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)