type 'a tree =
| Leaf of 'a
| Node of 'a * 'a tree * 'a tree
let rec foldtree init op = function
| Leaf c -> op c init init
| Node (c, l, r) -> op c (foldtree init op l) (foldtree init op r)
let size' = foldtree 0 (fun _ l r -> 1 + l + r) (* this compiles fine *)
let size'' = foldtree 0 (fun _ l r -> 1 + l + r) (* this doesn't *)
В приведенном выше коде OCaml определения size'
и size''
идентичны, но последнее вызывает ошибку компиляции:
Error: The type of this expression, '_weak1 tree -> int,
contains type variables that cannot be generalized