Simple debug help

We have the following, modeling the result of executing a Postgres command

type pg_result =
  | Err
  | Ok of string array array

This compiles:

Ok (x#get_all)

here, x is a Postgresql.result and #get_all returns a string array array
Link: postgresql-ocaml/postgresql.mli at master · mmottl/postgresql-ocaml · GitHub

However, the following does not compile:

Ok @@ x#get_all

I’m wondering what I’m doing wrong here, as my intuition is ocaml @@ is for "evaluate rhs until eol as expr, then pass it as arg to lhs

It’s correct that if f and x are expressions, f @@ x is equivalent to f x. But Ok is not an expression by itself, it needs to be always applied to its argument.

In other words, constructor application Ok x is not the same as function application f x, and @@ is a shortcut for the latter.

2 Likes

To paraphrase @emillon’s answer: Ok is a constructor, and constructors are not functions in OCaml, so it cannot be used as an argument to the @@ operator.

Cheers,
Nicolas

4 Likes

Thank you. This is where my mental model went wrong.