top | item 45809328

(no title)

ernst_klim | 3 months ago

> there is no equivalent to Java's toString or Rust's #[derive(Debug)]

There are devivers and a Format module for pretty printing.

https://github.com/ocaml-ppx/ppx_deriving

https://ocaml.org/manual/5.4/api/Format.html

discuss

order

quamserena|3 months ago

I am aware of this, but afaik it’s an external library? https://github.com/thierry-martinez/ppx_show

yawaramin|3 months ago

Re: the 'external lubrary' criticism. In Rust everybody says just use serde for serializing data and anyhow for error handling. But suddenly when we say use a ppx deriver in OCaml for effectively the same thing–serializing a type to a string–it's a huge problem. That's weird to me.

_flux|3 months ago

That's slightly disingenuous, as in the case of ppx_deriving you always need to state the type of the expression to call it, e.g.

    show_file
or

    [%derive.show: (int * int) list]
from the page. It's basically the same story for Format.

What many other languages do that if you have an object, you can just transform that object into a string or output it with debug formatting, without any particular support from the object. It isn't much of a chore, but it is some; and polymorphic functions now need one extra argument for the formatter.

I do recall there's a module for OCaml that can do "debug level" printing of objects by traversing their runtime structure, but that of course doesn't suite all objects.

At least in Rust it's not automatic that you can print an object in the first place :).