无法定义计算表达式自定义“条件”运算符

时间:2017-08-23 19:41:40

标签: f# computation-expression custom-operator

我在以下代码中提出condition的定义时遇到了严重困难。希望有一个例子和见解:

// a computation expression builder class
type Builder() =
    .
    .
    .

    [<CustomOperation( "condition",
      MaintainsVariableSpaceUsingBind = true )>]
    member this.Condition(p, [<ProjectionParameter>] b) = 
        condition p b

let attemp = AttemptBuilder()

let test =
    attempt { let x, y = exp1, exp2
              condition booleanExpr(x, y)   
              return (x, y) }

我认为b隐含( fun x, y -> booleanExpr(x, y) )。术语booleanExpr(x, y)只是一些涉及xy的布尔表达式。

1 个答案:

答案 0 :(得分:0)

找到它:

let condition p guard = ( fun () ->
    match p() with
    | Some x when guard x -> Some x
    | _ -> None )