4
votes

Right now I'm experimenting with F# computation expressions. General idea is to return control mechanism to drive actions executed after each step of recursive function call build from computation expression. Whole example can be seen here.

Using following example:

let rec loop () =
    actor {
        let! msg = m.Receive ()
        match msg with
        | "stop" -> return 0        // expected result: Return (0)
        | "unhandled" -> unhandled  // expected result: Unhandled 
        | x -> 
            mailbox.Sender() <! x
            return! loop ()         // expected result: (Become(fun m -> loop ()))
    }
loop ()

Unfortunately this ends with compile time error on unhandled: A custom operation may not be used in conjunction with 'use', 'try/with', 'try/finally', 'if/then/else' or 'match' operators within this computation expression.

Is it possible in any way to use custom operators inside match statements?

1
what is unhandled? And - assuming its of the right type - why don't you use return` or return! as well? - Random Dev
unhandled is a custom operator. In general Return and ReturnFrom are able to return discriminated union of type Return or Become depending on, if expression is a value or recursive function call. This DU is later used to apply additional logic. What I wanted to do, was to add additional DU type Unhandled that has another kind of logic applied. - Bartosz Sypytkowski

1 Answers

1
votes

I'm not sure what the details of the actor computation are, but if Unhandled is a value of the underlying computation type, you can certainly produce it using return!

Without knowing the details, I think something like this should work:

match msg with
| "stop" -> return 0
| "unhandled" -> return! Unhandled
| x -> 
    mailbox.Sender() <! x
    return! loop ()