2017-08-23 9 views
1

次のコードでは、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を含む単なるブール式です。

+2

あなたがここで達成しようとしていますか?また、[最小限の完全かつ検証可能なサンプルの作成方法](https://stackoverflow.com/help/mcve)を参照してください。 – scrwtp

+1

これはそれです...エキスパートF#4.0の475ページで、 'if' /' then'を置き換えるカスタム条件演算子のこの例を見ることができます。値「条件」は特に定義されていない。 –

答えて

0

はそれを見つけた:

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