Search code examples
ocamlocaml-core

Stdlib.Filename.extension error: This expression has type string but an expression was expected of type int


let get_path_elts path = 
  let rec walk_dir dirs = 
    match dirs with
    | [] -> []
    | dir::tail -> 
      match (stat dir).st_kind with
      | S_REG -> if Stdlib.Filename.extension dir = "ml" || Stdlib.Filename.extension dir = "mli" then dir::(walk_dir tail) else walk_dir tail
      | S_DIR -> List.concat [walk_dir (Array.to_list (Sys_unix.readdir dir)); (walk_dir tail)]
      | _ -> walk_dir tail
  in walk_dir [path]
;;

This is my code and I pasted it into utop. But it reports error:

Error: This expression has type string but an expression was expected of type
         int

This code is originally in a file in vscode but there's no error there. Wonder why.


Solution

  • In core, which I know you use because of your previous question, = is defined as int -> int -> bool.

    You need to use the string-specific equality operator, which you can bring into scope by locally opening String just for the predicate, for example:

    String.(Stdlib.Filename.extension dir = "ml")