开发者

Writing a function with type 'a -> string

For debugging purposes I'd like to have a function in O开发者_Python百科Caml that converts to string arbitrary type, the debugger currently has one, but it'd be cool to have one.

The sexplib library would be perfect, but the fact is that I can't modify all the types I need to add with sexp, and I can't use camlp4 either.

Is there any such function? (It won't be on production code so I accept dirty solutions)

Something like Haskell's Show typeclass would be exactly what I mean.

Thanks for your time


The Std module in Batteries Included provides a dump function which converts arbitrary types to readable strings. It is somewhat limited - as it does not know about types, it cannot print constructors for variant types properly and replaces them with numbers - but it can still be pretty helpful. Since type information is not available at runtime, that's about as good as you can do. The debugger and toplevel use compiler trickery to obtain better representations, but that is difficult if not impossible to do in a general library.

I seem to remember also seeing a more sophisticated dumping library somewhere, but I do not recall where.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜