6 ms·
> Where are the types? OCaml supports type annotations practically anywhere, if you want to add them. Alternatively, you can use an editor that supports queryi
by grumpyprole 3y ago
> Where are the types?
OCaml supports type annotations practically anywhere, if you want to add them. Alternatively, you can use an editor that supports querying or showing types for bindings. VSCode and Emacs support this.
> Remember recursion? How about linked lists?
A lot of beginner material uses List to introduce algebraic data types, inductive and equational reasoning - but you don't have to use them. In fact, the standard library encourages Seq instead over List.
It's a shame the author did not get past the basics, as there are many interesting comparisons to be made. For example, OCaml 5 effect handlers are a very interesting alternative to Rust's Async.
- lapinot 3y ago> In fact, the standard library encourages Seq instead over List. Uhu? Never heard about Seq before! Seems to have been introduced in 4.07 which was released 5 years ago, which is basically yesterday in terms of stdlib api. Set and Map, sure, but Seq? Lists are ubiquitous and they are indeed a central data structure in ocaml code. For anybody also wondering, Seq is a datatype for iterators (ie thunked lists).
- grumpyprole 3y agoThe Seq module has more functions than the List module. There are also more of_seq functions than of_list throughout. 5 years ago the compiler standard library was not a serious proposition, it is becoming more so now.
- yodsanklai 3y ago> the compiler standard library was not a serious proposition, it is becoming more so now. This is actually one of my grief with OCaml. I wish I didn't have to use Core. I don't look much at the std library anymore. My other grief is async (Lwt or Async). It forces to use monads which don't look very pretty in OCaml. That being said, I haven't looked at what OCaml 5.0 bring to the table on that front. This was my experience 5 years ago basically...
- sadiq 3y agoFor 5.0+ you might want to look at https://github.com/ocaml-multicore/eio https://github.com/ocaml-multicore/eio for how effects can make async much more pleasant
- sadiq 3y agoThere will also be DynArrays in the stdlib from 5.2: https://github.com/ocaml/ocaml/pull/11882 https://github.com/ocaml/ocaml/pull/11882
- dvektor 3y agoAuthor here: Totally hear you there, the post was literally just like it says 'first impressions'. Will most definitely be posting a few updates as I progress farther and try to make some comparisons :)
- grumpyprole 3y agoGlad to hear there is more to come! I do think OCaml 5 not only keeps the language relevant but also makes it even more interesting.
- MrJohz 3y ago> A lot of beginner material uses List to introduce algebraic data types, inductive and equational reasoning - but you don't have to use them. In fact, the standard library encourages Seq instead over List. I have repeatedly found this the hardest part of learning OCaml - it seems really difficult to find a tutorial that explains how to write OCaml as a real developer would working with production OCaml. Like, I don't need another explanation of sum types or recursion, these are things I'm already using regularly. I'm more interested in the things that OCaml does uniquely that I won't have seen in other languages. For example, which sequence type should I be using most often? And how often should I be using refs/mutability? And there's lots of alternate stdlibs - should I be dipping into those? What does unit testing typically look like for an OCaml project (and what about other forms of testing)? It's also very difficult to search for these sorts of things, because I feel like a lot of the results I get are for students learning OCaml as an introduction to functional programming, so questions like "how do I reverse a list" are answered by showing a clever recursive function that reverses a list, as opposed to the List.rev function. I am not the author, but I'm in a similar situation (right down the Rust background and trying out OCaml for AoC), and I'd love to get past the basics, but it's very difficult trying to find worthwhile resources to do that with.
- i_don_t_know 3y agoSome of your questions might be answered in this book (free online version): https://dev.realworldocaml.org/ https://dev.realworldocaml.org/
- dvektor 3y agoCool to see I'm not the only one in this situation... I want to like the language very much, so that is helping. That CIS310something or other course that is on Youtube is pretty cool, although a little slow for me (this is usually my issue with tutorials) but I did find the book online and that is great. I definitely hear you on the search results thing... I started declaring all my types manually just because that's what I'm used to but switched back just because I feel like matching the 'standard'. I will definitely admit to a dodgy AOC solution (hard-coding with magic values).. but overall I'm enjoying it quite a bit.
- 3y ago
- IshKebab 3y agoRight 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.