我可以使用and
关键字来设置相互递归的函数定义。我也可以and
用于相互递归的类型,但是如果类型和函数之间存在相互递归的关系怎么办?我唯一的选择是让函数成为类型的成员还是我也可以使用类似于and
这里的东西?
编辑:添加一个简化的伪示例,我希望能说明我正在尝试做的事情
// A machine instruction type
type Instruction = Add | CallMethod int (* method ID *) | ...
// A class representing a method definition
type MethodDef (fileName : string) =
member x.Params with get () = ...
member x.Body with get() =
let insts = readInstructions fileName
Array.map toAbstractInst insts
// a more abstract view of the instructions
and AbstractInstruction = AbstAdd | AbstCallMethod MethodDef | ...
// a function that can transform an instruction into its abstract form
let toAbstractInst = function
| Add -> AbstAdd
| CallMethod methodId -> AbstCallMethod (somehowResolveId methodId)
| ...
所以你可以在这里看到递归关系是相当间接地建立起来的: MethodDef <-> AbstractInst AND MethodDef -> toAbstractInst -> AbstractInstruction (其中 -> 表示“依赖于”)