Hacker News new | ask | show | jobs
by andrepd 2015 days ago
Tbf I find infix operators much more straightforward and readable. E.g.:

  let file = file |? raise Some_error
or

  let parse_result = parse <$> file
For the last two examples respectively.

EDIT: For completeness, the operators are

  let (|?) opt y =
    match opt with 
    | Some x -> x
    | None -> y

  let (<$>) f opt =
    match opt with
    | Some x -> Some (f x)
    | None -> None
1 comments

Infix operators are certainly more readable if you're familiar with the operators.

I imagine the possible ways of handling non-nullable types (and algebraic types as a whole) as a spectrum:

* TS style explicit if branches: Easy for beginners, annoying to experts

* Rust style methods on Option/Result/etc: Slightly confusing for beginners, somewhat elegant for experts

* Haskell/OCaml style infix operators: Confusing for beginners, elegant and easy for experts

Note that this is beginners to functional programming. Not necessarily beginners in programming as a whole. Plenty of smart people would get tripped up by a `<$>`

In my opinion, the problem with <$> is actually a problem with Haskell, which is that there's too damn many "operators" -- it's hard to keep track of them all. This also makes them hard to search for. When I search for "kotlin question mark colon", the first page of results is flooded with results about the Elvis operator (also a very memorable name for future searches, if I forgot). Searching for "haskell dollar sign in angle brackets" unearths nothing.

Something as common as option types (which is basically how nullable types are used in Kotlin) deserves special syntactic sugar, in my opinion. Granted many of Haskell's operators are also common, but I wish they'd been able to limit the variety a bit.

This is how you're supposed to search for Haskell operators:

https://www.stackage.org/lts-13.21/hoogle?q=%3C$%3E