我正在使用 with sexp
语法来自动生成s-exp函数。
问题在于,我使用sexplib打印的数据结构具有一些递归指针,并且打印会导致堆栈溢出。
因此,我需要覆盖to_sexp函数,并使其返回"(SomeRecursiveData)"
,我该如何实现?
注意:我的数据定义的格式为:
type somedata ...
and someotherdata ...
and this_is_problematic_recursive_data
and ....
with sexp
我正在使用 with sexp
语法来自动生成s-exp函数。
问题在于,我使用sexplib打印的数据结构具有一些递归指针,并且打印会导致堆栈溢出。
因此,我需要覆盖to_sexp函数,并使其返回"(SomeRecursiveData)"
,我该如何实现?
注意:我的数据定义的格式为:
type somedata ...
and someotherdata ...
and this_is_problematic_recursive_data
and ....
with sexp
let rec to_sexp = ...
而不是 let rec to_sexp = ..... and foo = .... calls to_sexp somewhere.....
),你可以尝试这个技巧:module A = struct type t with sexp end
module B = struct
include A
let to_sexp = .... your code ...
end
this_is_problematic_recursive_data
吗?或者它可以在之前定义吗?想法是在模块Foo
中定义类型为t
的this_is_problematic_recursive_data
,并且在同一模块中定义to_exp
。如果不可能的话,你应该考虑使用递归模块。 - Fabrice Le Fessant