Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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



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


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.


I’m not a fan of serde either. I wish there was a way to have foreign derives (or even better: compile-time reflection). There wasn’t a great way to serialize things in Rust in the standard library and the ecosystem crystalized around serde, which is suboptimal imo. There are libraries like facet[0] that implement general reflection, rather than just a subset of it, but it probably won't be supported by all of your dependencies.

[0] https://github.com/facet-rs/facet


This is kinda my point. I wonder why OCaml gets a full 'pain points' blog post, but Rust's error handling gets just a 'it's annoying but it's actually good'. Like I wonder why OCaml doesn't get a 'these parts are annoying but here is why they are actually good'. This is a pattern I see a lot with people coming from other languages (especially Rust). They have a tendency to over-emphasize the pain points and downplay the strengths (a single line at the end of the post, which is in my opinion the most important one–despite its flaws OCaml is the best choice for your use case).


Format is part of OCaml stdlib. You can write formatters for it manually.

Derivers are a library yes, it's pretty widespread if you need derive functionality, and it can derive basic functions. Proper link:

https://github.com/ocaml-ppx/ppx_deriving?tab=readme-ov-file...


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 :).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: