News for hackers. Stuff that matters.
Re:Two Years of OCaml by Georgelemental on Monday May 01, 2023 @09:28PM (#35781986)
> Type Inference is Bad

> So when you make a mistake, you’re still giving constraints to the inference engine. Erroneous type inferences propagate in every direction. Error messages appear hundreds of lines of code away from their origin.

Rust gets this right: it has type inference, but only within the function body. Most of your code is free of annotation cruft, but interfaces are strongly specified and errors can only propagate a certain distance.

Re:Two Years of OCaml by yawaramin on Tuesday May 02, 2023 @01:07AM (#35783577)
That's why it's idiomatic in OCaml to write interface files which contain type annotations for the modules. In practice many or most modules (over time) are given interface files which strongly contain the area of effect of type inference.
Re:Two Years of OCaml by IshKebab on Tuesday May 02, 2023 @04:17AM (#35784724)
Is it? I've only done a bit of OCaml (working on Sail) but that doesn't have any type interface files.
Re:Two Years of OCaml by rwmj on Tuesday May 02, 2023 @05:15AM (#35785062)
In virt-v2v we eventually enforced that every module file also has a corresponding interface file: https://github.com/libguestfs/virt-v2v/blob/master/check-mli...
Re:Two Years of OCaml by IshKebab on Tuesday May 02, 2023 @07:07AM (#35785767)
Probably if you need to do that it should just be enforced by the compiler. It's pretty clear to me (and the author of this post) that Rust got this right - small scale type inference is fine (in function bodies) but you really want explicit types for functions and up.

Without that, you lose out massively on readability and error message quality.

In Sail, I have IDE type inlays working but it barely helps with reachability because half the types are inferred as generic.

Re:Two Years of OCaml by rwmj on Tuesday May 02, 2023 @07:29AM (#35785956)
As a user of OCaml I have to disagree. Rust requires far too many type annotations and they are mostly unnecessary and clutter everything up (particularly with it's very heavy syntax). In OCaml my editor shows me types if I mouse over a variable, and if I need to document the code I can do that, but I don't need to.
Re:Two Years of OCaml by IshKebab on Wednesday May 03, 2023 @04:01AM (#35799419)
My point is it isn't very useful if half the types are 'a
Re:Two Years of OCaml by alasdair_a on Tuesday May 02, 2023 @08:00AM (#35786189)
We do have type interface files for almost every .ml file
Re:Two Years of OCaml by deredede on Tuesday May 02, 2023 @07:16AM (#35785854)
I know this is an unpopular position in the OCaml ecosystem, but I find .mli files to be in all ways inferior to the "inline" approach Rust (amongst others) take. OCaml requires perpetual context switches between .ml and .mli files, which can be alleviated with proper IDE tools but is still annoying, but most annoyingly requires writing every type definition and module signature twice.

On the other hand the OCaml/interface files approach is superior when you want to read documentation w/o implementation, but in practice using odoc is superior because you get hyperlinks.

In 10 years of using OCaml, I think everytime I interacted with mli I'd rather the author annotated types at the definition site.

Re:Two Years of OCaml by yawaramin on Tuesday May 02, 2023 @07:52AM (#35786135)
That's funny because I feel the opposite. An interface file saves me from having to wade through the implementation details and lets me focus on the exact parts that are relevant to me as a reader of the code. And yes odoc is nice too but the source of its usefulness usually comes from the fact that it's generated from a carefully curated interface file.
Re:Two Years of OCaml by eviks on Tuesday May 02, 2023 @09:25AM (#35787075)
Would type "dehints" be the best of both worlds: you IDE could hide all the types (just like with type hints it adds them) and you could toggle them to eg hide when reading the code? But then also you could toggle them back when needed instead of having to open an interface file?
Re:Two Years of OCaml by rwmj on Tuesday May 02, 2023 @09:49AM (#35787361)
OCaml IDEs including emacs can do that already, and yes it's a good thing.
Re:Two Years of OCaml by deredede on Tuesday May 02, 2023 @03:51PM (#35793176)
In my opinion the best of both worlds would just be to have Rust-style inline annotations and an "interface mode" that hides implementations (most modern IDEs can already do that) and non-public function.

BTW I am not sure why you would want to hide the types, I find them always useful.

Re:Two Years of OCaml by deredede on Tuesday May 02, 2023 @03:47PM (#35793119)
I agree that the usefulness of documentation systems such as odoc comes from a carefully curated interface, but I disagree with them needing a separate file which in my experience is cause of friction.

Other languages (I am familiar with Rust and Python) also have documentation systems that only show you public methods (and functions, types, etc.), and I find them equally useful as odoc. They just take the visibility information from inline annotations (which are enforced by the compiler, in the case of Rust) instead of a separate file.

Re:Two Years of OCaml by delta_p_delta_x on Tuesday May 02, 2023 @07:53AM (#35786142)
> I find .mli files to be in all ways inferior to the "inline" approach Rust (amongst others) take

Agreed. The same criticism can be levelled at C and C++ with header and implementation files.

Re:Two Years of OCaml by pharmakom on Tuesday May 02, 2023 @02:55AM (#35784248)
I disagree with the article here (and it’s a good article overall!) The compiler simply needs to be armed to detect this pattern and give better error messages. Vast majority of cases the user simply forgot an argument.
Re:Two Years of OCaml by dllthomas on Tuesday May 02, 2023 @03:41PM (#35793050)
* * *