Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I really want to like OCaml, as a more practical Haskell, but the syntax to me is just weird.

I think the ReasonML/ReScript guys had the right idea tbh, but I'm not interested in writing JS with it.



You can actually compile ReasonML to native code with dune.

What are some of the things that weird you out about OCaml's syntax?


  > You can actually compile ReasonML to native code with dune.
Yeah but the community doing native ReasonML is small and the maintenance has really petered off.

With OCaml 5 looming you're better off just biting the bullet and writing OCaml if you're going to do it IMO.

  > What are some of the things that weird you out about OCaml's syntax?
For whatever reason, I really dislike the named-argument syntax -- and some of the operators make it hard to read compared to named alternatives IMO.

Below examples showcases some of these:

    let variables = (json_variables :> (string * Graphql_parser.const_value) list)

    ~resolve:(fun info () ->
      Lwt_result.ok (Lwt_unix.sleep duration >|= fun () -> duration)
    )


OK but you can write code golf in any language, OCaml provides every opportunity to write readable code. The second snippet you provided is incomplete, if we were to write it in Python terms it would be something like:

    (resolve=lambda info: ...)
But let's say for the sake of argument that it's an argument to a function `f`:

    f ~resolve:(fun info () -> ...)
One of the simplest ways to improve readability is to use argument name punning:

    let resolve info () =
      let sleep =
        let open Lwt.Syntax in
        let+ () = Lwt_unix.sleep duration in
        duration
      in
      Lwt_result.ok sleep
    in
    f ~resolve


To be fair, it’s hardly code golf. It’s just that Lwt takes a monadic approach to concurrency and that’s always going to look a bit strange to someone unfamiliar with using monads and who doesn’t know that (>|=) is fmap.

I never understood why Lwt chose this aweful operator by the way. That’s a really unfortunate choice. My guess is that they really want people to use their ppx extension. I really hope that eio will solve the current concurrency libraries mess.

It’s the same for the first exemple: if you don’t know (:>) is upcasting, you are going to wonder what you are reading. I have to confess I had to look for it. Despite using Ocaml professionally for a bit a decade ago, I never had to upcast anything.

It’s hardly beautiful code but it’s fairly readable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: