Why can't the compiler match this type of function?

I have a problem with the OCaml compiler, I can’t explain myself. The following code will not compile:

open Postgresql

let get_nodes conn =
  ignore (conn#exec "SELECT * FROM node_full")

let () =
  let c = new connection () in
  ignore (get_nodes c)

It produces the following error:

File "test.ml", line 8, characters 20-21:
Error: This expression has type Postgresql.connection
       but an expression was expected of type < exec : string -> 'a; .. >
       Types for method exec are incompatible

(line 8 is the last line)

But the following code fragment compiles without errors (and works as expected in the full version of the code):

open Postgresql

let get_nodes (conn:connection) =
  ignore (conn#exec "SELECT * FROM node_full")

let () =
  let c = new connection () in
  ignore (get_nodes c)

The only difference is that I set the parameter type to conn in the get_nodes function.

Does anyone understand what is going on here? This is the first time I have to specify the type myself to make the code work, and I am an OCaml user every day ...

Additions, I do not see in the error message why the types used are incompatible, here is the type of the function exec:

method exec :
  ?expect:Postgresql.result_status list ->
  ?params:string array ->
  ?binary_params:bool array ->
  string -> Postgresql.result

and function type get_all from Postgresql.result:

method get_all : string array array

Happy New Year!

+4
source share
1

, , ocaml, , .

exec string -> 'a. exec Postgresql, . - , : conn. exec, , - :

ignore (
    (conn#exec :
        ?expect: 'a ->
        ?params: 'b ->
        ?binary_params: 'c ->
        string -> 'd) "SELECT * FROM node_full"
 )
+3

All Articles