1. All useful kinds of computation expressions encapsulate a lazy computation. The purpose of the Delay method is to lift a lazy computation from the regular expression language (where a lazy or delayed computation is conventionally represented as a function value of type unit -> 'a) to a value in the computation expression language. Delay is principally used when desugaring a computation expression block so that regular expressions inside the computation expression are not eagerly evaluated outside of it. For example, consider

1
b { return (Console.ReadLine()) }

Without Delay this would be desugared like so and the ReadLine would occur when the computation expression value is constructed and not when it is executed.

1
2
3
let m = b.Return(Console.ReadLine())
// ReadLine() executes here
b.Run(m)

Therefore we need the Delay to delay the evaluation of ReadLine until the computation expression is actually run (if indeed it is ever run).

1
2
3
let m = b.Delay (fun () -> b.Return(Console.ReadLine())
b.Run(m)
// ReadLine() executes here
By on 8/9/2008 1:15 AM ()

Thanks. I start to grok it now.

By on 8/9/2008 5:53 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper