在 match 语句中使用自定义计算表达式运算符

Using custom computation expression operator inside match statement

现在我正在试验 F# 计算表达式。总体思路是 return 控制机制来驱动从计算表达式构建的递归函数调用的每一步之后执行的动作。整个例子可见here.

使用以下示例:

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 ()

不幸的是,这以 unhandled 上的编译时错误结束:自定义操作不能与 'use'、'try/with'、[=25= 一起使用], 'if/then/else' 或 'match' 计算表达式中的运算符 .

是否可以通过任何方式在匹配语句中使用自定义运算符?

我不确定 actor 计算的细节是什么,但是如果 Unhandled 是基础计算类型的值,您当然可以使用 return! 生成它

在不知道细节的情况下,我认为这样的事情应该有效:

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