# Type generalization

This topic is 4429 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am defining a simple class, as follows:
type 'a tree =
| Nil
| Leaf of 'a
| Node of 'a tree array;;

class ['a] radix_map = object (self)
val mutable size = 0
val mutable tree : 'a tree = Node (Array.make 4 (Nil:'a tree))
method remove k =
let rec remove s = function
| Nil -> ( )
| Leaf _ -> size <- size - 1
| Node a -> let (i,s) = index (k,s) in
remove s a.(i);
if s < 0 || a.(i) = Node empty then a.(i) <- Nil
in remove (int_bits - 2) tree
end

The errors are:
The type of this class,
object
val mutable size : int
val mutable tree : '_a tree
method remove : int -> unit
end,
contains type variables that cannot be generalized

So, in effect, OCaml ignores both my type constraints that Nil and tree should be of type 'a tree (note that the 'a type is not free, it is bound by the class definition), and replaces it with a weak free type '_a. Why, and how can I go around this? EDIT: my stupidity knows no bounds. The error is that Empty is defined outside the class. However, the Why? question still stands: Why does O'Caml ignore my type constraints?

1. 1
2. 2
3. 3
4. 4
frob
20
5. 5
JoeJ
20

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632214
• Total Posts
3004853

×