4 ms·
Right but in practice most OCaml code does not have explicit types. Using an editor that shows type inlays (e.g. VSCode) helps a lot but not fully because a lo
by IshKebab 3y ago
Right but in practice most OCaml code does not have explicit types.
Using an editor that shows type inlays (e.g. VSCode) helps a lot but not fully because a lot of types are inferred as generics.
You're also correct that you don't have to use lists, but again most OCaml code does.
- ReleaseCandidat 3y ago> Using an editor that shows type inlays (e.g. VSCode) helps a lot but not fully because a lot of types are inferred as generics. That is because they are generic most of the time. This is also fun when using `#trace`in the REPL and all you see is `<poly>` instead of the "real" value
- IshKebab 3y agoRight but being overly generic makes them hard to understand. Generics are kind of like "compile time dynamic types" and they have many of the downsides of runtime dynamic types: * intent of the author not clear * auto-complete etc. doesn't work as well * type errors not caught as early (for generics this means you get super confusing type errors) Obviously some functions are intended to be generic (e.g. operations on containers) but typical OCaml code seems to contain functions that are actually only called with one type - and the author only intended for them to be called with one type - yet they get inferred as generic.
- ReleaseCandidat 3y ago> Right but being overly generic makes them hard to understand. Oh yes, I'm not arguing against that or your other points. I just wanted to express that the types being inferred as generic is not an error, but by design.
- IshKebab 3y agoRight, I didn't mean to imply otherwise. Just that that design has big downsides.
- grumpyprole 3y agoActually generic types can make reasoning about code much easier. This is the opposite of runtime dynamic types. Generic types tell you what the code doesn't (and cannot) do. Any generic type basically cannot be inspected or unpacked, see the paper "Theorems for free" for examples.
- yodsanklai 3y ago> Right but in practice most OCaml code does not have explicit types. Most OCaml code use .mli which does have types. This is the recommended practice but I noticed a lot of beginners coming from other languages are reluctant to use them (because of duplication). It's a simple idea but I think it's a strong point of OCaml and it makes easier to program "in the large" than Haskell for instance.
- ReleaseCandidat 3y agoBut .mli files do not help with the "no types in the source code" problém. You need LSP (Merlin) to "see" the types anyway. Only adding the signature in the samé (.ml) file helps against that problém. And I did not experience any advantage of separate signature files so far, but I also haven't written anything large in OCaml (>100 kLOC).
- yodsanklai 3y ago> But .mli files do not help with the "no types in the source code" problém It partially helps since it forces you to have types where they matters most: exported functions > And I did not experience any advantage of separate signature files so far, 100kLoc is already quite big! I'm starting to think I'm an outlier since a lot of people don't see the benefits :) For me, it helps because I really don't want to see the implementation when I use an API. If I need to look at the implementation, it means the interface isn't well specified. All I need should be in the interface: types, docs, (abstract) types. And no more. Typically, an .ml file will have more than what is exported, types won't be abstract but will have a concrete implementation, and type signatures may be missing. How would it feels like to use list if only https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.ml https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.ml was available, instead of https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.mli https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.mli? and that's not the most compelling example since it's a simple module where the main type isn't abstract. Generally speaking, giving up mli means giving up on encapsulation which is one of the primary tools to deal with large programs. Haskell forces you to say what is exported from a module, but the module user can only see the names. To see the signatures, you need to rely on generated doc or look it up on the code. Arguably, since OCaml has includes, it suffers from the same problem, your ".mli" may have tons of include and it becomes harder to see what's exported without an external tool
- grumpyprole 3y ago> You're also correct that you don't have to use lists, but again most OCaml code does. If the list is not being used in a critical section, what's the problem? The implication here is that OCaml is slow or inefficient. OCaml is often orders of magnitude faster than other high-level languages (e.g. Python). Arrays in OCaml can result in some very fast code.