News for hackers. Stuff that matters.
Re:What I wish I knew when learning OCaml (2018) by pmoriarty on Tuesday May 03, 2022 @05:34AM (#31246358)
I tried OCaml a long time ago, and one of the things that really turned me off of it was all the inscrutable error messages. I went to #ocaml on Freenode for help, and when I had the error messages explained to me I asked how the person who explained them knew what they meant. He told me that the reason he knew was because he took a couple of semesters of type theory courses at his university. I didn't want to have to take a couple of semesters of type theory courses in order to be able to program effectively in this language. I hope the situation has improved since then.

The other thing I didn't like (which was something shared with other statically-typed languages) was feeling like I had to wrestle forever with the compiler to get my program to run. It just always felt so much easier to write programs in dynamically typed languages. Sure, my programs might have bugs in them, but I could iron them out over time, and my programs change so much anyway that pieces of buggy-but-working code in those dynamically typed languages might be replaced wholesale anyway before I even ran in to the bugs.. so the pace of prototyping in dynamically typed languages is much faster, in my experience.

Re:What I wish I knew when learning OCaml (2018) by yodsanklai on Tuesday May 03, 2022 @05:55AM (#31246475)
I think both of these points can be alleviated with certain coding habits that come with practice. For instance, adding type annotations in some places will help with error messages. And using "assert false" (which has type 'a) will let you run an incomplete/broken program.

As for the error messages requiring expertise in type theory, it sounds as an exaggeration, esp. when not using advanced features.

Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @06:42AM (#31246739)
Hey, thanks for that "assert false" trick! I never knew that!

I don't know much about type theory but I did eventually manage to learn to interpret OCaml's type errors well enough to get things to compile. It still feels like driving a car by scraping it along a highway guardrail but it's often better than testing. I have to write twice as much code as in Python, it takes me longer to get it to run, and I still can't call it from C or Lua, but it runs twenty times as fast as Python and the process of thinking through the types helps me a lot to write code that works.

I keep hoping I'll internalize the type system enough that my programs run the first time they way they do in Python, but it hasn't happened yet.

I still often write a prototype in Python, though.

Re:What I wish I knew when learning OCaml (2018) by legobmw99 on Tuesday May 03, 2022 @09:00AM (#31247725)
OCaml also has a built in function failwith: string -> ‘a, so you can print an error in those cases. If it’s a code path you ever think will execute, failwith “some error here” is probably better than assert false
Re:What I wish I knew when learning OCaml (2018) by pmoriarty on Tuesday May 03, 2022 @05:58AM (#31246487)
"adding type annotations in some places will help with error messages."

I annotated the hell out of my programs, and completely avoided OCaml's type inference as much as I could because I saw that it could not guess what I meant. I still had tons of problems understanding the error messages.

Error messages like "This expression is of type X but an expression was expected of type X" were not uncommon, and super frustrating.

Re:What I wish I knew when learning OCaml (2018) by WastingMyTime89 on Tuesday May 03, 2022 @06:09AM (#31246540)
> I annotated the hell out of my programs, and completely avoided OCaml's type inference as much as I could because I saw that it could not guess what I meant.

The type checker and the type inference parts of the compiler are one and the same. If the type checker can’t infer what you mean you are most likely writing invalid code.

> Error messages like "This expression is of type X but an expression was expected of type X" were not uncommon, and super frustrating.

Ocaml is a strongly typed static language. As such your code has to respect type constraints. It’s very much a feature not a bug.

Still to be less defensive I too sometimes wish the error messages were more clear in highlighting why a certain type was expected but at least Ocaml provides tooling to live check the inferred type of any sub-expression while you edit your code.

Re:What I wish I knew when learning OCaml (2018) by pmoriarty on Tuesday May 03, 2022 @06:17AM (#31246592)
"you are most likely writing invalid code"

Of course I'm writing invalid code: that's why I'm getting error messages. The problem is that I'm having trouble understanding why it's invalid. Error messages are supposed to help me here, but quite often they didn't.. they just led to more confusion... especially in complex code, where I needed help and clarity most.

Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @06:39AM (#31246727)
> Ocaml is a strongly typed static language. As such your code has to respect type constraints. It’s very much a feature not a bug.

That's reasonable if the error message were "This expression is of type X but an expression was expected of type Y". But the worst error message in the OCaml interactive toplevel is when they're the same. This happened when you defined a new type named X (which, incidentally, must be lowercase), but there were still references to things of the old type X.

I think this has been fixed in recent versions of OCaml.

Re:What I wish I knew when learning OCaml (2018) by skissane on Tuesday May 03, 2022 @07:57AM (#31247228)
You can get the same thing in Java essentially: two classes with the same name but loaded by different class loaders are incompatible. You’d get errors at runtime that you can’t cast X to X. I think they’ve made the exception messages more helpful in more recent Java versions, but in older versions the errors were very confusing to newbies because they didn’t mention the class loader difference which was the actual cause of them
Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @08:08AM (#31247305)
In OCaml I think this could only happen with the interpreter (e.g., an interactive toplevel). At least today the compiler complains if you try to define two types with the same name in the same context.

The problem is that it would happen often in the interpreter when you were interactively trying stuff out, because when you're trying stuff out, you change the definitions of things.

Python actually sort of has the same problem not only when you use an interactive interpreter but even when you reload a module: the new class definitions don't modify the old one, they just get bound to the same name. So it's easy to end up with two alglayout.Vbox classes or two diff.Formula classes in the same interactive interpreter at the same time. But it's much less of a problem in Python because Python usually doesn't care what class things are, just what methods they define, so objects belonging to both classes can coexist peacefully. The usual exception is when you have an isinstance check somewhere.

Re:What I wish I knew when learning OCaml (2018) by poorlydefined on Tuesday May 03, 2022 @07:31AM (#31247033)
Yeah if you did this now you’d get something like “this expression is of type t/1 but an expression of type t/2” to disambiguate.
Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @07:36AM (#31247062)
The fact that this problem took 25 years to fix is maybe the more damning thing. It wasn't a bug, exactly; it was a usability problem. Clearer demonstrations of development priorities could hardly be given.
Re:What I wish I knew when learning OCaml (2018) by octachron on Tuesday May 03, 2022 @09:58AM (#31248383)
That was mostly a man-power issue. The good news is that nowadays such nonsensical error messages are near the top of my personal development priority for OCaml.
Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @10:08AM (#31248483)
During those 25 years OCaml gained native-code compilers for new architectures, labeled arguments, and polymorphic variants, among other things. How could it be mostly a manpower issue?
Re:What I wish I knew when learning OCaml (2018) by octachron on Tuesday May 03, 2022 @10:43AM (#31248950)
Work and time contributed by open source collaborators on their free or academic time cannot be magically converted from one subsystem to another. It is honestly very easy to have progress on the aspects that spark interest while some subsystem are starved from attention when there are no full-time developers working on a project.
Re:What I wish I knew when learning OCaml (2018) by kragen on Tuesday May 03, 2022 @12:17PM (#31250101)
That's true, but that doesn't mean there was no manpower to fix that error message; it meant that fixing it wasn't a priority to the people who were working on OCaml at the time. You're just offering an explanation for why it wasn't a priority: fixing it didn't spark their interest.
Re:What I wish I knew when learning OCaml (2018) by octachron on Tuesday May 03, 2022 @09:55AM (#31248352)
And the REPL should warn you that if you have a value of type "t/n" it is probably a left-over from a previous definition of type t in the session.
Re:What I wish I knew when learning OCaml (2018) by kripke on Wednesday May 04, 2022 @05:14AM (#31258267)
There is a balance to find between not enough annotations and too much annotations that one has to find. My algorithm is to write few annotations by default, and my answer to error messages I do not understand is to add "obvious" annotations (e.g. type of arguments) to tell type inference what I expect. Rarely does that solve the underlying issue, but it often makes the error message much clearer.

With experience, I am more or less able to preemptively add the annotations that will probably help (e.g. aiming avoid accidentally polymorphic functions, the return type of mutually recursive functions, etc.).

I don't think I have ever gotten "This expression is of type X but an expression was expected of type X" outside of the toplevel with type re-definitions, and recent OCaml versions have a nice error message for this now:

    Error: This expression has type t/1 but an expression was expected of type
             t/2
           Hint: The type t has been defined multiple times in this toplevel
             session. Some toplevel values still refer to old versions of this
             type. Did you try to redefine them?
In general the error messages have been continuously improving in recent years.
Re:What I wish I knew when learning OCaml (2018) by gmfawcett on Tuesday May 03, 2022 @07:27AM (#31247007)
> Error messages like "This expression is of type X but an expression was expected of type X" were not uncommon, and super frustrating.

That particular error message has been gone from OCaml for years now.

Re:What I wish I knew when learning OCaml (2018) by baby on Tuesday May 03, 2022 @07:12AM (#31246910)
Ah! Didn't know the `assert false` trick. I believe `failwith "..."` will not always allow you to run the program
Re:What I wish I knew when learning OCaml (2018) by mhitza on Tuesday May 03, 2022 @06:03AM (#31246505)
I guess inscrutable error messages are a "feature" of ML-like languages. I have not used OCaml yet, but the first few years with Haskell probably have been just as rough in terms of parsing type errors.

Haskell has a few niceties when trying to debug type errors nowadays. For example typed holes that allows one to spice in `_` within code and the compiler will tell you what type it expects you to replace that hole with, alleviating many issues I had previously. But in the same sense I'm in agreement with you, and I've been more productive when writing Guile/Chicken Scheme code for small tools.

Interestingly I've binged on OCaml videos recently, because I would find it interesting to work with it some day. From a distance what I like about it (aside from the functional language goodness) is that it supports objects, has row polymorphism, optional arguments, and imperative constructs (I sure like my mutation heavy for/while loops when prototyping code).

Re:What I wish I knew when learning OCaml (2018) by bmitc on Tuesday May 03, 2022 @10:38AM (#31248888)
I haven't actually used OCaml in any depth, but I have found that F# tends to have quite good error messages. In the Programming Languages Coursera course, I found that SML has pretty bad error messages.