我正在为建模者简化计划定义创建一个计算表达式(CE)。我想定义只在CE中可用的函数。在这个例子中,编译器说自定义操作
报告的错误是关于
step
和branch
的使用是错误的,但我不明白为什么。编译器只是说它们没有被正确使用。
注意我知道我可以在CE之外定义
step
和branch
来实现这个。这个问题明确是关于使用自定义操作符。我想将这个逻辑隔离起来,只在CE的上下文中可用。
type Step =
| Action of string
| Branch of string list list
type Plan =
{
Name : string
Steps : Step list
}
type PlanBuilder () =
member _.Yield _ =
{
Name = ""
Steps = []
}
member _.Run state = state
[<CustomOperation "name">]
member _.Name (state, name) =
{ state with Name = name }
[<CustomOperation "steps">]
member _.Steps (state, steps) =
{ state with Steps = steps }
[<CustomOperation "step">]
member _.Step (state, step) =
Action step
[<CustomOperation "branch">]
member _.Branch (state, branch) =
Branch branch
let plan = PlanBuilder ()
let x =
plan {
name "Chicken"
steps [
// The compiler reports errors for all the
// `step` and `branch` calls
step "1"
step "2"
branch [
[
step "3a"
step "4a"
]
[
step "3b"
step "4b"
]
]
step "5"
]
}
报告的错误是关于
step
的。
FS3095: 'step'的使用不正确。这是查询或计算表达式中的自定义操作。
I.e.:
{}
按钮或 Ctrl+K,则代码块将缩进四个空格,从而使其呈现为代码。 - Chris