[Solved] Notty.term: signature error with a simple function

I have this simple code, that is a inspired from the life.ml example of Notty. This idea is to create the simplest code possible that could illustrate:

  • the creation of a Notty.term that is updated with a timeout
  • the creation of 2 threads, one for the interface and one for another loop.

Here is the code:

(* ocamlfind ocamlc -o double_threads -package lwt,notty.lwt -linkpkg -g double_threads.ml*)
open Lwt
open Lwt.Infix
open Notty
open Notty_lwt
open Notty.Infix

module Term = Notty_lwt.Term

let counter = ref 0

let rec increase_counter () =
  (
    if !counter < max_int then counter := (!counter + 1)
    else counter := 0
  );
  Lwt.return ()
  >>= fun () ->
    increase_counter ()

let render (w, h) =
  I.(strf ~attr:A.(fg lightblack) "[counter %d]" !counter)

let timer () = Lwt_unix.sleep 0.1 >|= fun () -> `Timer

let event term = Lwt_stream.get (Term.events term) >|= function
  | Some (`Resize _ | #Unescape.event as x) -> x
  | None -> `End

let rec loop term (e, t) dim =
  (e <?> t) >>= function
  | `End | `Key (`Escape, []) ->
      Lwt.return_unit
  | `Timer ->
      Term.image term (render dim)
      >>= fun () ->
        loop term (e, timer ()) dim
  | `Mouse ((`Press _|`Drag), (x, y), _) ->
      loop term (event term, t) dim
  | `Resize dim' ->
      Term.image term (render dim')
      >>= fun () ->
        loop term (event term, t) dim
  | _ -> loop term (event term, t) dim

let interface () =
  let tc = Unix.(tcgetattr stdin) in
  Unix.(tcsetattr stdin TCSANOW { tc with c_isig = false });
  let term    = Term.create () in
  let size = Term.size term in
  loop term (event term, timer ()) size

main () =
  Lwt.join [
    interface ();
    increase_counter ();
  ]

I get the following error when I compile it:

ocamlfind ocamlc -o double_threads -package lwt,notty.lwt -linkpkg -g double_threads.ml
File "double_threads.ml", line 51, characters 2-6:
Error: This function has type
         Term.t ->
         ([> `End
           | `Key of Notty.Unescape.key
           | `Mouse of Notty.Unescape.mouse
           | `Paste of Notty.Unescape.paste
           | `Resize of int * int
           | `Timer ]
          as 'a)
         Lwt.t * 'a Lwt.t -> int * int -> unit Lwt.t
       It is applied to too many arguments; maybe you forgot a `;'.

I have not been able to find where the problem is even with Merlin, I am sure that it is a stupid error but I can not find it.

I’m not sure if it’s an error from copying and pasting, but you’re missing a let in front of main.

Indeed, the missing let seems like the problem.

@hcarty, @ejgallego,

okaayy :sweat_smile:, it is time to do a break I guess. thanks

1 Like