The reason is that the right-hand-side of a "let! ident=" is a normal expression, not a computation expression, so as in your original code, you need to introduce another async{} block to get back into an async workflow.

See below (third example) for I think maybe the best way to write it (similar to your first example).

Start a reply to this message with the 'Quote' button to see how to format code well on hubfs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 

// your orig
let interactiveFunction maxI f f_zero =
    MailboxProcessor.Start(fun inbox ->
    let rec loop x i =
        async { 
            let! tryMsg =
                if i < maxI then
                    async {
                        let! msg = inbox.TryReceive(timeout=0)
                        return msg
                    }
                else
                    async {
                        let! msg = inbox.Receive()
                        return Some(msg)
                    }
            match tryMsg with
            | None -> return! loop (f x) (i+1)
            | Some msg -> ()
        }
    loop f_zero 0) 

// your orig attempt
let interactiveFunction maxI f f_zero =
    MailboxProcessor.Start(fun inbox ->
    let rec loop x i =
        async {
            let! tryMsg =
                if i < maxI then
                    inbox.TryReceive(timeout=0)
                else
                    let! msg = inbox.Receive()  // does not compile
                    Some(msg)
            match tryMsg with
            | None -> return! loop (f x) (i+1)
            | Some msg -> ()
        }
    loop f_zero 0) 

// best?
let interactiveFunction maxI f f_zero =
    MailboxProcessor.Start(fun inbox ->
    let rec loop x i =
        async { 
            let! tryMsg =
                if i < maxI then
                    inbox.TryReceive(timeout=0)
                else
                    async {
                        let! msg = inbox.Receive()
                        return Some(msg)
                    }
            match tryMsg with
            | None -> return! loop (f x) (i+1)
            | Some msg -> ()
        }
    loop f_zero 0) 

By on 7/20/2009 3:37 PM ()
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