3 ms·
It's similar to Rust in certain respects, but it feels very different in several others. * There's no ownership tracking. This might be obvious - it doesn't ne
by wk_end 5y ago
It's similar to Rust in certain respects, but it feels very different in several others.
* There's no ownership tracking. This might be obvious - it doesn't need it, it has GC - but is actually kind of unfortunate; even in GC'd languages, linearity can be used to safely model state machines/typestate or for optimization.
* There's no traits/ad-hoc polymorphism at all.
* There's no impls, though a common pattern with a similar feel is to define types inside modules with the actual implementation of the type hidden, and provide an interface via functions to create or interact with instances of the type.
* It uses exceptions for error handling (when it doesn't use an option type) and even as a lightweight control flow mechanism. And now there's algebraic effects as well.
* It has an extraordinarily powerful module system, with functors (pass modules into other modules to produce new modules) and first-class modules (modules that exist as values at run-time that can be passed around to functions).
* It has some more advanced type machinery - GADTs, polymorphic variants, and a very interesting, powerful, almost completely unused object system.
* This bleeds a little into "ecosystem" and a little into "no ad-hoc polymorphism" and a little into the oddness of the syntax and a little into subjectivity, but in general doing really bog-standard imperative stuff in OCaml, while possible, tends to feel more awkward than it does in Rust.
- edwintorok 5y ago* exceptions for error handling: you can also use the 'result' type (an OK of 'a|Error of 'b type in the stdlib) if you don't like exceptions. At least the function's signatures will give more details in how it can fail and force you to handle the failures (or pass them through to your caller). Unfortunately you lose backtraces that way (though you can use wrappers like reword_error, or logging to give more details on what goes wrong, but backtraces can be convenient in locating the source of an error sometimes). A compromise is to use a wrapper like trap_exn (from Bos) that puts the exception with its backtrace in the Error type and then you propagate that...
- baby 5y ago* There’s no explicit typing also, everything is polymorphic by default * Compiler errors won’t help you much * The tooling is lacking compared to cargo. It’s pretty much makefiles in a lisp language (better love parens) * The ordering of your code matters, shadowing functions and structs is a a thing * use A::* is pretty much the default, and transitive dependencies are imported at the top level, so be ready to be confused about the origin of a function or module or variable I really wish more people would write OCaml so that we’d have a better ecosystem. If you’re looking to get into a new language, you should seriously consider it as there are SO MANY low hanging fruits and opportunities to have an impact on the language.