简体   繁体   中英

OCAML module contains type variables that cannot be generalized

Code:

  let size = 10
  let getTbl = Array.init size ~f:(fun _ -> Avltree.empty )
end 

Error:

Error: The type of this module,
       sig val size : int val getTbl : ('_weak1, '_weak2) Avltree.t array end,
       contains type variables that cannot be generalized

How do I let the Ocaml compiler know that I plan to store both my key's and values as ints?

Have tried a few different approaches - none of which have worked.

Weak type variables denote types that are not yet inferred, usually because you have defined a program variable and never used it, so the type checker has no idea what this variable contains. It is fine, in general, as the first usage of the variable will define its type. However, since the whole type checking routine in OCaml is bounded by the scope of a compilation unit (ie, a file), such variables should be defined before you compile your file.

Therefore, you have to either (1) use the variable, (2) constraint it to some type, eg, (let getTbl : (int, int) Avltree.t array) .. in the implementation (.ml) file, or (3) in the mli file. You can even just create an empty .mli file (with the same name as you .ml file) and this will automatically hide all variables defined in your module and enable compilation.

Avltree.empty更改为(Avltree.empty : (int, int) Avltree.t)可能会起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM