I'm a beginner in F#, so it's a simple question and maybe a duplicate, but I couldn't find the answer anywhere...
I'm reading this LOGO DSL implementation and I don't understand, what is the meaning of the "do!" notation in here:
this.Loaded.Add (fun _ -> async { do! Async.Sleep 200 for cmd in theDrawing do do! this.Execute(cmd) } |> Async.StartImmediate )
Can you help?
I'll only add that the do!
notation doesn't have to be explicitly supported by the computation expression, because the same thing can be written using let!
like this:
do! foo() // Using do! let! _ = foo() // Equivalent using let!
In general let!
us used when you have some function implemented using computation expressions and you want to call it from another computation expression of the same type. This means, that it is used for composing computation expressions. For async
this composition means that you have a non-blocking asynchronous code and call it from another asynchronous workflow in some special way to make the call asynchronous.
The let!
keyword allows you to do this and get some value as the result, while do!
is a shortcut that you can use if the computation doesn't return anything.
A chapter from Real-world Functional Programming that discusses computation expressions (and also sequence expressions) is available as a free sample, so if you want to read a more detailed tutorial about computation expressions this may be a good source of infromation:
BTW: It should be possible to write the sample code in your question in a nicer way using the AwaitEvent
primitive like this:
async { let! _ = this.Loaded |> Async.AwaitEvent do! Async.Sleep 200 for cmd in theDrawing do do! this.Execute(cmd) } |> Async.StartImmediate
This means the same thing - it first waits until the Loaded
event occurs, then it waits 200ms and then it does the rest of the work. This waiting is special (that's why we use let!
/do!
, because it doesn't block the thread while waiting).
F# computation expressions (a.k.a. "workflows") use the syntax
builder { expression }
where expression
can contain special constructs including the various "bang" keywords like let!
and do!
. Like LINQ in C# or VB, F# computation expressions are just a syntactic sugar (that desugars into method calls on the builder
).
One of the most common types of computation expression is async
, as described here.
In this particular example, the async
is being used along with Async.Sleep
to temporarily get off the UI thread, to give the UI a chance to redraw, react to mouse events, etc. This general technique is described more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With