Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a function with type 'a -> string

Tags:

ocaml

For debugging purposes I'd like to have a function in OCaml 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

like image 211
FCo Avatar asked Feb 07 '11 21:02

FCo


People also ask

How do you create a function in a string?

Use the Function() Constructor to create a function from the string. It accepts any number of arguments(in form of string). Last one should be the body of the function. In this example, Only the body of the function is passed which is returning a value.

What does () => void mean TypeScript?

According to the TypeScript docs: void represents the return value of functions which don't return a value. Whenever you see a function returning void , you are explicitly told there is no return value. All functions with no return value have an inferred return type of void .


1 Answers

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.

like image 84
Michael Ekstrand Avatar answered Sep 22 '22 04:09

Michael Ekstrand