4 ms·
Politely, I feel like this is the issue with OCaml as a community. You gave a beautiful answer about programming language design and composition. But how does i
by hardwaregeek 3y ago
Politely, I feel like this is the issue with OCaml as a community. You gave a beautiful answer about programming language design and composition. But how does it feel to write the language? How can you print a user defined data type? From reading around, it seems like your options are: explicitly pass a print function for that specific type, use a third party library for printing, or (my "favorite") don't. Or how does it feel to write a function that takes a generic that can be printed, checked for equality, and read? Or to convert one type into another type?
And we're still talking about semantics here. How does it feel to use the language server? How does it feel to read the code? To build the language? Ultimately that's what users judge a language on. Yes, Rust in some ways has a worse form of abstraction. It is global, not fully generic and doesn't allow for overloading. But it creates a user experience that is nicer. It lets a user print something and compare two variables and do type conversions without having to scratch their head, read a forum post and import a library.
- yodsanklai 3y ago> How can you print a user defined data type? Pretty much the same way you'd do in Haskell or Rust: you add a `[@@deriving show]` directive to your datatype.
- ywei3410 3y ago> You gave a beautiful answer about programming language You do the same thing as in Rust, Scala or Haskell and derive the printer [1]. Then at the callsite, if you know the type then you do `T.show` to print it or `T.eq`. If you don't know the type, then you pass it in at the top level as a module and then do `T.show` or `T.eq`. > Or to convert one type into another type? If you want to convert a type, then you have a type that you want to convert from such as foo and bar, then you do `Foo.to_bar value`. We can keep going, but you can get the point. You _can't_ judge a language by doing what you want to do with one language in another. If I judge Rust by writing recursive data structures and complaining about performance and verbosity that's not particularly fair correct? I can't say that Dart is terrible for desktop because I can't use chrome developer tools on its canvas output and ignore it's hot-reloading server. I can't say Common Lisp code is unreadable because I don't have type annotations and ignore the REPL for introspection. [1] https://github.com/ocaml-ppx/ppx_deriving https://github.com/ocaml-ppx/ppx_deriving
- hardwaregeek 3y agoOkay...so you have to add a third party library to print? Yes, recursive data structures are a pain in Rust (well, recursive multi-owner data structures). But that's like comparing changing your oil to opening your car door. We do one of these a lot more. And bear in mind, I had to find this answer by googling and reading a random forum post. That's some pretty poor documentation. And that's not talking about aesthetics. `T.show foo` or `Foo.to_bar value` is a lot of syntactic overhead for a rather common task. And again, these are the lesser concerns to the other ones I outlined. Reading the code, building the code, and understanding the compiler errors are the big ones.
- yawaramin 3y agoYou don't need a third-party library to derive a pretty-printer for your types, it just makes it simpler. OCaml takes the approach that the core compiler doesn't do any codegen, that is left to PPXs (basically, middlewares which transform code) which run before the compiler. You can write a pretty-printer yourself using the conventions of the `Format` module. Is it manual and annoying? Yeah. That's why the PPX exists. It's a tradeoff.
- ywei3410 3y agoNot really third-party; ocaml-ppx is something similar to `javax` in the Java space? But it is optional and doesn't come bundled with OCaml. Recursive structures are only rare in Rust _because_ they suck to write and have terrible performance characteristics in the language. In languages like Haskell, OCaml and Scala using tagless initial encodings for eDSL's [2] are really, really common. I don't write Rust like I write any semi-functional language with function composition because, well, it's _painful_ and not a good fit for the language. > `T.show foo` or `Foo.to_bar value` is a lot of syntactic overhead The `T.()` opens a scope so OCaml code generally looks like: ``` let foo = T.(if equal zero x then show x else "not here") ``` for, ``` fn foo(t: T) -> String { if(t == T::ZERO) { t.show() } else { "not here".into() } } ``` Even when talking about usage, each language community for better or worse has a predefined workflow in mind. Your questions and workflow are specific to a particular language; you don't, for example, ask about the REPL, the compile times, debugging macros, the cold-start of the compiler, whether hot-reloading is a thing, the debugger, how it interacts with perf, how to instrument an application. Presumably because the language you use or the programs that you make don't have those components as part of their main workflow. [1] https://github.com/ocaml-ppx https://github.com/ocaml-ppx [2] https://peddie.github.io/encodings/encodings-text.html https://peddie.github.io/encodings/encodings-text.html